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 main
import "fmt"
func main() {
fmt.Println("Test")
}
三、產生可執行檔(以我機器為例,具體可參考官網文檔):
編譯:8g -o test.8 test.go
連結:8l -o test.exe test.8
執行test.exe,會輸出:
Test
四、批量產生可執行檔
如果寫的測試代碼多的話,每一次都要輸入兩遍命令,感覺很不方便。
所以我決定寫一個指令碼,讓它自動遍曆目前的目錄下所有以".go"結尾 的檔案,對檔案進行編譯產生目標檔案、連結產生可執行檔,然後刪除目標檔案。這個指令碼是仿照之前的文章(http://www.jb51.net/article/61951.htm)中產生Makefile的原理寫的,功能有限,適合寫測試代碼的時候用。
這裡是代碼(python指令碼):
複製代碼 代碼如下:
'''
File : compileGo.py
Author : Mike
E-Mail : Mike_Zhang@live.com
'''
import os
srcSuffix = '.go'
dstSuffix = '.exe'
cmdCompile = "8g"
cmdLink = "8l"
fList = []
for dirPath,dirNames,fileNames in os.walk('.'):
for file in fileNames:
name,extension = os.path.splitext(file)
if extension == srcSuffix :
fList.append(name)
tmpName = name + '.8' # temp file
strCompile = '%s -o %s %s ' % (cmdCompile,tmpName,file)
print strCompile
os.popen(strCompile) # compile
strLink = '%s -o %s %s' % (cmdLink,name+dstSuffix,tmpName)
print strLink
os.popen(strLink) # link
os.remove(tmpName) # remove temp file
break # only search the current directory
好,就這些了,希望對你有協助。