windows下用go語言寫程序
linux下,google的go語言安裝起來很方便,用起來也很爽,幾行代碼就可以實現很強大的功能。
現在的問題是我想在windows下玩……
其實windows下也不麻煩,具體見下文。
一、安裝go語言:
1、安裝MinGW(https://bitbucket.org/jpoirier/go_mingw/downloads)
2、下載源碼
進入C:\MinGW,雙擊mintty開啟終端窗口;
執行"hg clone -u release https://go.googlecode.com/hg/ /c/go"下載源碼;
3、編譯源碼
執行"cd /c/go/src"進入src目錄,執行"./all.bash"進行編譯;
4、設置環境變量
編譯完成后,會在C:/go/bin/下生成二進制文件,在PATH中加入"C:\go\bin;";
二、寫go代碼:
文件:test.go
代碼如下:
package mainimport "fmt"
func main() { fmt.Println("Test") }</pre>
</div>三、生成可執行文件(以我機器為例,具體可參考官網文檔):
編譯:8g -o test.8 test.go
鏈接:8l -o test.exe test.8
執行test.exe,會輸出:Test
四、批量生成可執行文件
如果寫的測試代碼多的話,每一次都要輸入兩遍命令,感覺很不方便。
所以我決定寫一個腳本,讓它自動遍歷當前目錄下所有以".go"結尾 的文件,對文件進行編譯生成目標文件、鏈接生成可執行文件,然后刪除目標文件。這個腳本是仿照之前的文章(http://www.cnblogs.com/MikeZhang/archive/2012/01/17/2324567.html)中生成Makefile的原理寫的,功能有限,適合寫測試代碼的時候用。
這里是代碼(python腳本):1 ''' 2 File : compileGo.py 3 Author : Mike 4 E-Mail : Mike_Zhang@live.com 5 ''' 6 import os 7 8 srcSuffix = '.go' 9 dstSuffix = '.exe' 10 cmdCompile = "8g" 11 cmdLink = "8l" 12 13 fList = [] 14 for dirPath,dirNames,fileNames in os.walk('.'): 15 for file in fileNames: 16 name,extension = os.path.splitext(file) 17 if extension == srcSuffix : 18 fList.append(name) 19 tmpName = name + '.8' # temp file 20 strCompile = '%s -o %s %s ' % (cmdCompile,tmpName,file) 21 print strCompile 22 os.popen(strCompile) # compile 23 strLink = '%s -o %s %s' % (cmdLink,name+dstSuffix,tmpName) 24 print strLink 25 os.popen(strLink) # link 26 os.remove(tmpName) # remove temp file 27 break # only search the current directory</div>好,就這些了,希望對你有幫助。
</span>本文由用戶 just1 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!