詳解Golang編譯成DLL文件
下面由golang教學欄位來介紹Golang 編譯成 DLL 檔案的方法,希望對需要的朋友有幫助!
golang 編譯 dll 過程中需要用到 gcc,所以先安裝 MinGW。
windows 64 位元系統應下載MinGW 的64 位元版本: https://sourceforge.net/projects/mingw-w64/
下載後執行 mingw-w64-install.exe,完成MingGW 的安裝。
首先寫 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
golang 中的string 參數在C# 中可以如下引用:
public struct GoString { public string Value { get; set; } public int Length { get; set; } public static implicit operator GoString(string s) { return new GoString() { Value = s, Length = s.Length }; } public static implicit operator string(GoString s) => s.Value; }
// func.go package main import "C" import "fmt" //export Add func Add(a C.int, b C.int) C.int { return a + b } //export Print func Print(s *C.char) { /* 函数参数可以用 string, 但是用*C.char更通用一些。 由于string的数据结构,是可以被其它go程序调用的, 但其它语言(如 python)就不行了 */ print("Hello ", C.GoString(s)) //这里不能用fmt包,会报错,调了很久... } func main() { }
編譯
#go build -ldflags " -s -w" -buildmode=c-shared -o func.dll func.go
還是有點大的,880KB,純C 編譯的只有48KB,應該是沒有包含全部的依賴吧,go是全包進來了
Go 呼叫
package main import ( "fmt" "syscall" ) func main() { dll := syscall.NewLazyDLL("func.dll") add := dll.NewProc("Add") prt := dll.NewProc("Print") r, err, msg := add.Call(32, 44) fmt.Println(r) fmt.Println(err) fmt.Println(msg) name := C.CString("Andy") prt.Call(uintptr(unsafe.Pointer(name))) }
out: 76 0 The operation completed successfully. Hello Andy
Python 呼叫
from ctypes import CDLL, c_char_p dll = CDLL("func.dll") dll.Add(32, 33) dll.Print(c_char_p(bytes("Andy", "utf8")))
C 呼叫
#include <iostream> #include <windows.h> using namespace std; typedef int(*pAdd)(int a, int b); typedef void(*pPrt)(char* s); int main(int argc, char *argv[]) { HMODULE dll= LoadLibraryA("func.dll"); pAdd add = (pAdd)GetProcAddress(dll, "Add"); pPrt prt = (pPrt)GetProcAddress(dll, "Print"); cout << add(321, 33) << endl; prt("Andy"); FreeLibrary(dll); return 0; }
更多相關技術文章,請訪問go語言教學欄位!
以上是詳解Golang編譯成DLL文件的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

在Go中安全地讀取和寫入檔案至關重要。指南包括:檢查檔案權限使用defer關閉檔案驗證檔案路徑使用上下文逾時遵循這些準則可確保資料的安全性和應用程式的健全性。

如何為Go資料庫連線配置連線池?使用database/sql包中的DB類型建立資料庫連線;設定MaxOpenConns以控制最大並發連線數;設定MaxIdleConns以設定最大空閒連線數;設定ConnMaxLifetime以控制連線的最大生命週期。

可以透過使用gjson函式庫或json.Unmarshal函數將JSON資料儲存到MySQL資料庫中。 gjson函式庫提供了方便的方法來解析JSON字段,而json.Unmarshal函數需要一個目標類型指標來解組JSON資料。這兩種方法都需要準備SQL語句和執行插入操作來將資料持久化到資料庫中。

GoLang框架與Go框架的差異體現在內部架構與外部特性。 GoLang框架基於Go標準函式庫,擴充其功能,而Go框架由獨立函式庫組成,以實現特定目的。 GoLang框架更靈活,Go框架更容易上手。 GoLang框架在效能上稍有優勢,Go框架的可擴充性更高。案例:gin-gonic(Go框架)用於建立RESTAPI,而Echo(GoLang框架)用於建立Web應用程式。

後端學習路徑:從前端轉型到後端的探索之旅作為一名從前端開發轉型的後端初學者,你已經有了nodejs的基礎,...

FindStringSubmatch函數可找出正規表示式匹配的第一個子字串:此函數傳回包含匹配子字串的切片,第一個元素為整個匹配字串,後續元素為各個子字串。程式碼範例:regexp.FindStringSubmatch(text,pattern)傳回符合子字串的切片。實戰案例:可用於匹配電子郵件地址中的域名,例如:email:="user@example.com",pattern:=@([^\s]+)$獲取域名match[1]。

Go框架開發常見問題:框架選擇:取決於應用需求和開發者偏好,如Gin(API)、Echo(可擴展)、Beego(ORM)、Iris(效能)。安裝和使用:使用gomod指令安裝,導入框架並使用。資料庫互動:使用ORM庫,如gorm,建立資料庫連線和操作。身份驗證和授權:使用會話管理和身份驗證中間件,如gin-contrib/sessions。實戰案例:使用Gin框架建立一個簡單的部落格API,提供POST、GET等功能。

Go語言中使用預先定義時區包含下列步驟:匯入"time"套件。透過LoadLocation函數載入特定時區。在建立Time物件、解析時間字串等操作中使用已載入的時區,進行日期和時間轉換。使用不同時區的日期進行比較,以說明預先定義時區功能的應用。
