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 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.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

好,就這些了,希望對你有幫助。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM