发布于 2016-01-24 04:35:19 | 143 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的GO语言教程,程序狗速度看过来!

Go语言

Go是一种新的语言,一种并发的、带垃圾回收的、快速编译的语言。Go是谷歌2009年发布的第二款编程语言。2009年7月份,谷歌曾发布了Simple语言,它是用来开发Android应用的一种BASIC语言。


这篇文章主要介绍了Windows下使用go语言写程序安装配置实例,本文讲解了安装go语言、写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"结尾 的文件,对文件进行编译生成目标文件、链接生成可执行文件,然后删除目标文件。
这里是代码(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

好,就这些了,希望对你有帮助。



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务