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
代碼如下:
import "fmt"
func main() {
fmt.Println("Test")
}
三、生成可執行文件(以我機器為例,具體可參考官網文檔):
編譯:8g -o test.8 test.go
鏈接:8l -o test.exe test.8
執行test.exe,會輸出:
Test
四、批量生成可執行文件
如果寫的測試代碼多的話,每一次都要輸入兩遍命令,感覺很不方便。
所以我決定寫一個腳本,讓它自動遍歷當前目錄下所有以".go"結尾 的文件,對文件進行編譯生成目標文件、鏈接生成可執行文件,然后刪除目標文件。這個腳本是仿照之前的文章中生成Makefile的原理寫的,功能有限,適合寫測試代碼的時候用。
這里是代碼(python腳本):
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
新聞熱點
疑難解答