如何在win7环境下搭建Go开发环境

Python026

如何在win7环境下搭建Go开发环境,第1张

首先需要下载下载go平台安装包

安装程序 下载地址址h如果是您的系统是windows32 位系统请选择go1.3.3.windows-386.msi即可,其他的,请按照自己所需版本来进行下载,如下图所示:

安装以及配置环境变量

由于Windows下的的安装包有两种:msi和zip的;zip的是免安装的,解压在配置一些环境变量之后就可以使用,msi的则是安装包版本的,安装的时候会设置好对应的环境变量。我的电脑是Win7 64位,因为方便,就下载了下面的版本。下好安装包后,安装过程就很简单了,一路“Next”就好了。 虽然msi会自动配置一些环境变量,但是本人在安装完之后还是自己配置了环境变量,所以在安装完Go之后,我们最好还是检查一些所有的环境变量是否正常。主要的环境变量有以下几个:

GOROOT:Go的安装目录

GOPATH:用于存放Go语言Package的目录,这个目录不能在Go的安装目录中

GOBIN:Go二进制文件存放目录,写成%GOROOT%\bin就好

GOOS:操作系统

GOARCH:指定系统环境,i386表示x86,amd64表示x64

PATH:需要将%GOBIN%加在PATH变量的最后,方便在命令行下运行Go

如下图所示:

像我自己安装的,下载完成之后解压到任意目录(所有目录均不能使用中文):D:\Go;

然后是go环境变量的配置:

GOARCH:386(go安装版本)

GOBIN:D:\Go\bin(exe执行文件路径)

GOOS:windows(go运行的系统)

GOROOT:D:\Go(go的解压路径)

GOPATH:E:\go\data(go的工具包路径,随意指定,后面会用到)

然后在path环境变量中追加:%GOBIN%

完成之后在cmd窗口输入:go version,如下图所示:

1、解压压缩包到go工作目录,如解压到E:\opensource\go\go,解压后的目录结构如下:

E:\opensource\go\go

├─api

├─bin

│ ├─go.exe

│ ├─godoc.exe

│ └─gofmt.exe

├─doc

├─include

├─lib

├─misc

├─pkg

├─src

└─test

2、增加环境变量GOROOT,取值为上面的go工作目录

3、Path环境变量中添加"%GOROOT%\bin",以便能够直接调用go命令来编译go代码,至此go编译环境就配置好了

注:如果不想手动设置系统环境变量,也可下载go启动环境批处理附件,

修改goenv.bat文件中的GOROOT值为上面的go工作目录后直接双击该bat文件,go编译环境变量即设置完成。

4、测试go编译环境,启动一个cmd窗口,直接输入go,看到下面的提示就是搭建成功了

E:\opensource\go\go>go

Go is a tool for managing Go source code.

Usage:

go command [arguments]

The commands are:

build compile packages and dependencies

clean remove object files

doc run godoc on package sources

env print Go environment information

fix run go tool fix on packages

fmt run gofmt on package sources

get download and install packages and dependencies

install compile and install packages and dependencies

listlist packages

run compile and run Go program

testtest packages

toolrun specified go tool

version print Go version

vet run go tool vet on packages

Use "go help [command]" for more information about a command.

Additional help topics:

gopath GOPATH environment variable

packagesdescription of package lists

remote remote import path syntax

testflagdescription of testing flags

testfuncdescription of testing functions

Use "go help [topic]" for more information about that topic.

5、编译helloworld测试程序,go语言包中test目录带有helloworld.go测试程序,源码见"附一 helloworld.go",

直接调用"go build helloworld.go"就生成了"helloworld.exe"可执行程序,运行一下这个程序看到了我们期望的hello,wolrd。

E:\opensource\go\go\test>go build helloworld.go

E:\opensource\go\go\test>helloworld.exe

hello, world

E:\opensource\go\go\test>

附一 helloworld.go

// cmpout

// Copyright 2009 The Go Authors. All rights reserved.

// Use of this source code is governed by a BSD-style

// license that can be found in the LICENSE file.

// Test that we can do page 1 of the C book.

package main

func main() {

print("hello, world\n")

}