go语言学习笔记(一)--go语言编译环境搭建

来源:互联网 发布:淘宝泻油汤真相 编辑:程序博客网 时间:2024/05/18 06:20

一、编译器下载

下载地址:http://golangtc.com/download,分安装版本(msi)和解压版本(rar,zip等),根据自己系统下载相应版本编译器,例如我是win7 64位系统,我下载的是


二、编译器安装

go编译器安装很简单,只需要把下载的压缩包解压即可,解压后生成一个go目录,目录结构如下:


进入bin目录可以看到三个exe文件,其中go.exe就是传说中的go编译器了,有点类似linux下的gcc:


三、go环境变量配置及编译运行

有了go.exe我们就可以用它来编译一个go代码文件了,为了能在cmd窗口方便使用go命令,我们需要配置以下go编译器的环境变量,当然你也可以进入go.exe所在目录,直接执行go.exe,在环境配置path中加入go.exe所在的目录,我是win7系统,配置方法如下:


新建一个go源代码文件,命名为main.go,键入以下内容,并保存到工作目录中,假设为C:\Users\weigk\Desktop\go

package mainimport "fmt"func main() {    fmt.Println("hello world")}
</pre><p><img src="http://img.blog.csdn.net/20140823130554319" alt="" /></p><p>在cmd命令行界面进入main.go所在目录,并执行 go build main.go, 此命令为编译main.go,然后在main.go所在的目录会生成一个main.exe,这个就是最终生成的可执行文件了,或者直接执行go run main.go,此命令为编译并运行main.go,可以看到屏幕上输出“hello world”</p><p><img src="http://img.blog.csdn.net/20140823131119283" alt="" /></p><p><strong>四、开发工具搭建</strong></p><p><span style="font-weight:bold; white-space:pre"></span>目前比较流行的开发工具有两种,一种为notepad++,另一种为LiteIde,LiteIde是专门为go语言开发的集成工具,下面分别介绍notepad++和LiteIde的搭建</p><p><span style="white-space:pre"></span>1.notepad++</p><p><span style="white-space:pre"><span style="white-space:pre"></span>下载notepad++ 并安装,notepad++默认不支持go语法高亮,但go语言本身提供了notepad++的go语言解析文件,共两个配置文件:<span style="color: rgb(51, 51, 51); line-height: 28px; text-indent: 16px; white-space: pre; "><span style="font-family:SimSun;font-size:12px;">go安装目录\misc\notepadplus\userDefineLang.xml和<span style="color: rgb(51, 51, 51); font-family: SimSun;font-size:12px; line-height: 28px; text-indent: 16px; white-space: pre; ">go安装目录\misc\notepadplus\go.xml</span></span></span>可以通过notepad++导入语言包的方式进行配置:</span></p><p><span style="white-space:pre"><img src="http://img.blog.csdn.net/20140824155326106" alt="" /></span></p><p><span style="white-space: pre;">第一步,去掉<span style="color: rgb(51, 51, 51); font-family: SimSun;font-size:12px; line-height: 28px; text-indent: 16px; white-space: pre; ">userDefineLang.xml里面开头的注释,注意</span></span><span style="color: rgb(51, 51, 51); font-family: SimSun;font-size:12px; line-height: 28px; text-indent: 16px; white-space: pre; ">,不是删掉,而是把注释放开</span></p><p><span style="color: rgb(51, 51, 51); font-family: SimSun;font-size:12px; line-height: 28px; text-indent: 16px; white-space: pre; "><img src="http://img.blog.csdn.net/20140824155257453" alt="" /></span></p><p><span style="white-space: pre;">第二步,选择notepad++的 语言->自定义语言->导入文件 并导入<span style="color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Tahoma, Arial, STXihei, 'Microsoft YaHei', 微软雅黑, sans-serif; font-size: 16px; line-height: 28px; white-space: pre; "><span style="color: rgb(51, 51, 51); font-family: SimSun;font-size:12px; line-height: 28px; text-indent: 16px; white-space: pre; ">userDefineLang.xml</span></span></span></p><p><span style="white-space: pre;"><span style="color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Tahoma, Arial, STXihei, 'Microsoft YaHei', 微软雅黑, sans-serif; font-size: 16px; line-height: 28px; white-space: pre; "><span style="color: rgb(51, 51, 51); font-family: SimSun;font-size:12px; line-height: 28px; text-indent: 16px; white-space: pre; "><img src="http://img.blog.csdn.net/20140824155525453" alt="" /></span></span></span></p><p><span style="white-space: pre;"><span style="color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Tahoma, Arial, STXihei, 'Microsoft YaHei', 微软雅黑, sans-serif; font-size: 16px; line-height: 28px; white-space: pre; "><span style="color: rgb(51, 51, 51); font-family: SimSun;font-size:12px; line-height: 28px; text-indent: 16px; white-space: pre; ">第三步,拷贝go.xml到 notepad++安装目录下的\plugins\APIs下,然后重启notepad++,点击语言选项,会发现多了一个go语言选项</span></span></span></p><p><span style="white-space: pre;"><span style="color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Tahoma, Arial, STXihei, 'Microsoft YaHei', 微软雅黑, sans-serif; font-size: 16px; line-height: 28px; white-space: pre; "><span style="color: rgb(51, 51, 51); font-family: SimSun;font-size:12px; line-height: 28px; text-indent: 16px; white-space: pre; "><img src="http://img.blog.csdn.net/20140824160154730" alt="" /></span></span></span></p><p><span style="white-space: pre;"><span style="color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Tahoma, Arial, STXihei, 'Microsoft YaHei', 微软雅黑, sans-serif; font-size: 16px; line-height: 28px; white-space: pre; "><span style="color: rgb(51, 51, 51); font-family: SimSun;font-size:12px; line-height: 28px; text-indent: 16px; white-space: pre; ">第四步,编辑环境配好了,但为了方便go语言的编译,还需要配置notepad++对go的编译支持,这一步很简单:</span></span></span></p><p><span style="white-space: pre;"><span style="color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Tahoma, Arial, STXihei, 'Microsoft YaHei', 微软雅黑, sans-serif; font-size: 16px; line-height: 28px; white-space: pre; "><span style="color: rgb(51, 51, 51); font-family: SimSun;font-size:12px; line-height: 28px; text-indent: 16px; white-space: pre; ">选择notepad++的插件->PlugeManager->Show PlugeManager然后找到Gonpp,并勾选,点击install</span></span></span></p><p><span style="white-space: pre;"><span style="color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Tahoma, Arial, STXihei, 'Microsoft YaHei', 微软雅黑, sans-serif; font-size: 16px; line-height: 28px; white-space: pre; "><span style="color: rgb(51, 51, 51); font-family: SimSun;font-size:12px; line-height: 28px; text-indent: 16px; white-space: pre; "><img src="http://img.blog.csdn.net/20140824160602546" alt="" /></span></span></span></p><p><span style="white-space: pre;"><span style="color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Tahoma, Arial, STXihei, 'Microsoft YaHei', 微软雅黑, sans-serif; font-size: 16px; line-height: 28px; white-space: pre; "><span style="color: rgb(51, 51, 51); font-family: SimSun;font-size:12px; line-height: 28px; text-indent: 16px; white-space: pre; ">安装过程中会有一个提示框,无视它,选择继续安装即可,然后notepad++会自动重启,重启后会报一个错误:</span></span></span></p><p><span style="white-space: pre;"><span style="color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Tahoma, Arial, STXihei, 'Microsoft YaHei', 微软雅黑, sans-serif; font-size: 16px; line-height: 28px; white-space: pre; "><span style="color: rgb(51, 51, 51); font-family: SimSun;font-size:12px; line-height: 28px; text-indent: 16px; white-space: pre; "><img src="http://img.blog.csdn.net/20140824160858785" alt="" /></span></span></span></p><p><span style="white-space: pre;"><span style="color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Tahoma, Arial, STXihei, 'Microsoft YaHei', 微软雅黑, sans-serif; font-size: 16px; line-height: 28px; white-space: pre; "><span style="color: rgb(51, 51, 51); font-family: SimSun;font-size:12px; line-height: 28px; text-indent: 16px; white-space: pre; ">按照提示,是说你notepad++找不到go.exe安装位置,需要配置一下GoRoot环境变量,然后再重启notepad++就不会报错了</span></span></span></p><p><span style="white-space: pre;"><span style="color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Tahoma, Arial, STXihei, 'Microsoft YaHei', 微软雅黑, sans-serif; font-size: 16px; line-height: 28px; white-space: pre; "><span style="color: rgb(51, 51, 51); font-family: SimSun;font-size:12px; line-height: 28px; text-indent: 16px; white-space: pre; "><img src="http://img.blog.csdn.net/20140824161705756" alt="" /></span></span></span></p><p style="text-indent: 16px;"><span style="font-family:SimSun;font-size:12px;color:#333333;"><span style="line-height: 28px; white-space: pre;">至此,notepad++的go开发环境就完成了,新建一个main.go,输入以下内容:</span></span></p><p style="text-indent: 16px;"><span style="font-family:SimSun;font-size:12px;color:#333333;"><span style="line-height: 28px; white-space: pre;"></span></span><pre name="code" class="plain">package mainimport "fmt"func main(){fmt.println("hello world")}

然后点击插件->Gonpp->go run,可以看到右边的输出窗口,输出hello world,哈哈,大功告成!

2.LiteIde

LiteIDE的下载网站为:http://sourceforge.net/projects/liteide/files/,下载后解压安装即可。


使用前,同样需要配置一下go的环境变量,选择查看->编辑环境变量,一般只需要配置GOROOT GOBIN GOARCH GOOS四个变量即可

GOROOT是Go的安装目录,GOBIN是go.exe所在目录,需要注意的是,GOARCH GOOS是目标文件的运行环境,而并非本机环境,这对于不同的运行环境来说,编译是非常方便的

具体LiteIDE的使用方式,还在进一步探索中~~~

五、总结

在配置notepad++成功后,我新建了两个文件,main.go 和 test.go,其中test.go中有一个函数,我是想在main.go中调用这个函数,可折腾了半天都提示找不到这个函数,后来才猛然发现,原来问题出在函数大小写上,也就是说,在一个go文件 中的函数,如果有外部调用需求的话,这个函数命一定要大写,小写代表是本文件私有函数,不能被外部调用!!!go语言的语法真够严格的

0 0
原创粉丝点击