首先撰寫 golang 程序 exportgo.go:
~~~
package main
import "C"
import "fmt"
//export PrintBye
func PrintBye() {
fmt.Println("From DLL: Bye!")
}
//export Sum
func Sum(a int, b int) int {
return a + b;
}
func main() {
// Need a main function to make CGO compile package as C shared library
}
~~~
編譯成 DLL 文件:
~~~
go build -buildmode=c-shared -o exportgo.dll exportgo.go
~~~
編譯后得到 exportgo.dll 和 exportgo.h 兩個文件。
參考 exportgo.h 文件中的函數定義,撰寫 C# 文件 importgo.cs:
~~~
using System;
using System.Runtime.InteropServices;
namespace HelloWorld
{
class Hello
{
[DllImport("exportgo.dll", EntryPoint="PrintBye")]
static extern void PrintBye();
[DllImport("exportgo.dll", EntryPoint="Sum")]
static extern int Sum(int a, int b);
static void Main()
{
Console.WriteLine("Hello World!");
PrintBye();
Console.WriteLine(Sum(33, 22));
}
}
}
~~~
編譯 CS 文件得到 exe
~~~
csc importgo.cs
~~~
將 exe 和 dll 放在同一目錄下,運行。
~~~
>importgo.exe
Hello World!
From DLL: Bye!
55
~~~

- Go語言基礎篇
- Go語言簡介
- Go語言教程
- Go語言環境安裝
- Go語言結構
- Go語言基礎語法
- Go語言數據類型
- Go語言變量
- Go語言提高篇
- Go語言實現貪吃蛇
- Go 諺語
- 解決連通性問題的四種算法
- golang 幾種字符串的連接方式
- Go JSON 技巧
- Go += 包版本
- Golang 編譯成 DLL 文件
- Go指南:牛頓法開方
- Go語言異步服務器框架原理和實現
- Golang適合高并發場景的原因分析
- 如何設計并實現一個線程安全的 Map ?(上篇)
- go語言執行cmd命令關機、重啟等
- IT雜項
- IT 工程師的自我管理
- IT界不為人知的14個狗血故事
- Go語言版本說明
- Go 1.10中值得關注的幾個變化
- Golang面試題解析
- Golang面試題
- Golang語言web開發
- golang 模板(template)的常用基本語法
- go語言快速入門:template模板
- Go Template學習筆記
- LollipopGo框架
- 框架簡介
- Golang語言版本設計模式
- 設計模式-單例模式
- Golang語言資源下載
- 公眾賬號
- leaf
- 合作講師
- 公開課目錄