輕松入門:使用Golang開發(fā)跨平臺GUI應(yīng)用
基礎(chǔ)操作
requires:Go 1.11.x or later
安裝依賴
go get github.com/lxn/walk go get github.com/akavel/rsrc rsrc
創(chuàng)建文件 go-gui/gui.go
package main import ( "strings" "github.com/lxn/walk" . "github.com/lxn/walk/declarative" ) func main() { var inTE, outTE *walk.TextEdit MainWindow{ Title: "xiaochuan測試", MinSize: Size{600, 400}, Layout: VBox{}, Children: []Widget{ HSplitter{ Children: []Widget{ TextEdit{AssignTo: &inTE, MaxLength: 10}, TextEdit{AssignTo: &outTE, ReadOnly: true}, }, }, PushButton{ Text: "SCREAM", OnClicked: func() { outTE.SetText(strings.ToUpper(inTE.Text())) }, }, }, }.Run() }
go mod init go-gui
創(chuàng)建后綴為manifest的文件
go-gui/go-gui.exe.manifest
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0"> <assemblyIdentity version="1.0.0.0" processorArchitecture="*" name="SomeFunkyNameHere" type="win32"/> <dependency> <dependentAssembly> <assemblyIdentity type="win32" name="Microsoft.Windows.Common-Controls" version="6.0.0.0" processorArchitecture="*" publicKeyToken="6595b64144ccf1df" language="*"/> </dependentAssembly> </dependency> <application xmlns="urn:schemas-microsoft-com:asm.v3"> <windowsSettings> <dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2, PerMonitor</dpiAwareness> <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">True</dpiAware> </windowsSettings> </application> </assembly>
rsrc -manifest go-gui.exe.manifest -o rsrc.syso go build -ldflags="-H windowsgui"
雙擊 go-gui.exe
文件go-gui.exe可以復(fù)制到任何地方直接運行,因為已經(jīng)將manifest文件編譯進去了。
添加圖標(biāo)
rsrc -manifest go-gui.exe.manifest -ico favicon.ico -o rsrc.syso go build -ldflags="-H windowsgui"
favicon.ico為下載的圖標(biāo)文件,效果如下。
運行程序,任務(wù)欄上的圖標(biāo)也變成了這個。
具體說明
1、manifest 文件
這個東西的作用就是為了解決 以前windows上的“Dll 地獄” 問題才產(chǎn)生的新的DLL管理解決方案。大家知道,Dll是動態(tài)加載共享庫,同一個Dll可能被多個程序所使用,而所謂“Dll 地獄”就是當(dāng)不通程序依賴的Dll相同,但版本不同時,由于系統(tǒng)不能分辨到底哪個是哪個,所以加載錯了Dll版本,然后就掛了。于是蓋茨就吸取了教訓(xùn),搞了一個程序集清單的東西,每個程序都要有一個清單,這個清單存再和自己應(yīng)用程序同名的.manifest文件中,里面列出其所需要的所有依賴,這兒所列出的依賴可不是簡單地靠文件明來區(qū)分的,而是根據(jù)一種叫做“強文件名”的東西區(qū)分的。。。。。。
2、rsrc程序的作用
rsrc - Tool for embedding binary resources in Go programs. Generates a .syso file with specified resources embedded in .rsrc section, aimed for consumption by Go linker when building Win32 excecutables. The generated *.syso files should get automatically recognized by 'go build' command and linked into an executable/library, as long as there are any *.go files in the same directory. OPTIONS: -arch string architecture of output file - one of: 386, amd64, [EXPERIMENTAL: arm, arm64] (default "amd64") -ico string comma-separated list of paths to .ico files to embed -manifest string path to a Windows manifest file to embed -o string name of output COFF (.res or .syso) file; if set to empty, will default to 'rsrc_windows_{arch}.syso'
rsrc將靜態(tài)資源文件編譯到syso文件中,go語言在最新版本中已經(jīng)支持syso文件的鏈接并且會搜索當(dāng)前目錄,自動鏈接。
在Go1.16起引入了embed包,也可以實現(xiàn)將靜態(tài)文件編譯進去。參考:https://www.php.cn/be/go/484192.html
本機安裝的是Go1.14,沒有辦法演示使用 embed 的方式加載 manifest 文件。
當(dāng)然,我們也可以不將 manifest文件編譯進去,比如,我的目錄下只有g(shù)ui.go和go-gui.exe.manifest文件,然后使用go build -ldflags="-H windowsgui"來編譯。得到go-gui.exe,注意,此時的manifest文件名一定要是go-gui.exe.manifest。點擊go-gui.exe,效果一樣,只是,go-gui.exe和go-gui.exe.manifest要在同一個目錄下。
3、參數(shù)-ldflags="-H windowsgui"的意義
正常情況下,任何語言開發(fā)的GUI都會伴隨著一個CMD框在后面,比如這樣
是的,如果僅僅使用go build來編譯的話,就是這個效果,帶上參數(shù)-ldflags="-H windowsgui"就會去掉后面的CMD框。
這個CMD框的作用是打印信息,輸出等。
另外,還可以壓縮一下生成的程序的大?。?ldflags="-H windowsgui -w"
關(guān)于 golang GUI
win
win把win32 api幾乎所有相關(guān)的聲明都導(dǎo)成了go。如果發(fā)現(xiàn)有沒導(dǎo)出的,可以參照里面的方法導(dǎo)出,很簡單的。
walk
是對win包的再次封裝,方便使用。
fyne
文檔相對豐富一些。
govcl
總結(jié):
- Golang是一種強大的編程語言,可以用于開發(fā)GUI桌面應(yīng)用。
- 通過使用Golang的GUI庫,開發(fā)人員可以輕松地創(chuàng)建具有豐富用戶界面和交互功能的應(yīng)用程序。
- Golang的并發(fā)性和高性能使其成為開發(fā)桌面應(yīng)用的理想選擇。
- Golang提供了豐富的標(biāo)準庫和第三方庫,可以幫助開發(fā)人員快速構(gòu)建跨平臺的應(yīng)用程序。
- 此外,Golang的靜態(tài)類型和自動垃圾回收機制使得開發(fā)過程更加穩(wěn)定和可靠。
無論是開發(fā)桌面應(yīng)用程序還是移動應(yīng)用程序,Golang都是一個強大的選擇。通過使用Golang開發(fā)GUI桌面應(yīng)用,開發(fā)人員可以享受到高效、可靠和易于維護的開發(fā)體驗。
到此這篇關(guān)于輕松入門:使用Golang開發(fā)跨平臺GUI應(yīng)用的文章就介紹到這了,更多相關(guān)golang開發(fā)GUI桌面應(yīng)用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
golang架構(gòu)設(shè)計開閉原則手寫實現(xiàn)
這篇文章主要為大家介紹了golang架構(gòu)設(shè)計開閉原則手寫實例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-07-07Golang基礎(chǔ)學(xué)習(xí)之map的示例詳解
哈希表是常見的數(shù)據(jù)結(jié)構(gòu),有的語言會將哈希稱作字典或者映射,在Go中,哈希就是常見的數(shù)據(jù)類型map,本文就來聊聊Golang中map的相關(guān)知識吧2023-03-03