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.2cto.com/kf/201203/122315.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

好,就这些了,希望对你有帮助。
E-Mail : Mike_Zhang@live.com

 摘自 MikeZhang的博客 

相关内容

    暂无相关文章

评论关闭