windows 怎么编译 go语言

Python017

windows 怎么编译 go语言,第1张

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")

}

是Go语言吗?

Go 编译过程 九个步骤

第一步. all.bash

% cd $GOROOT/src

% ./all.bash

第一步 all.bash 只是调用了另外两个 shell 脚本:make.bash 和run.bash。若使用 Windows 或 Plan9,其过程也基本类似,只是脚本分别以 .bat 或 .rc 结尾。在文章的其他部分,请用适当的操作系统对应的扩展来补全命令。

第二步. make.bash

. ./make.bash --no-banner

make.bash 作为 all.bash 内容的一部分,如果它退出也会中断构建过程

第三步. cmd/dist

gcc -O2 -Wall -Werror -ggdb -o cmd/dist/dist -Icmd/dist cmd/dist/*.c

当健全检查完成后,make.bash 开始编译 cmd/dist。

第四步. go_bootstrap

现在 go_bootstrap 已经构建完成,make.bash 的最后一步是使用 go_bootstrap 编译完整的 Go 标准库,包括一个完整的 go 工具用以替换。

echo "# Building packages and commands for $GOOS/$GOARCH."

"$GOTOOLDIR"/go_bootstrap install -gcflags "$GO_GCFLAGS" \

-ldflags "$GO_LDFLAGS" -v std

第五步. run.bash

现在 make.bash 已经完成,回到 all.bash 的执行,这会调用 run.bash。run.bash 的任务是编译和测试标准库、运行时以及语言测试集。

bash run.bash --no-rebuild

由于 make.bash 和 run.bash 都会调用 go install -a std,因此需要使用 –no-rebuild 标志来避免重复前面的步骤,–no-rebuild 跳过了第二个 go install。

# allow all.bash to avoid double-build of everythingrebuild=trueif [ "$1" = "--no-rebuild" ]then shiftelse echo '# Building packages and commands.' time go install -a -v std echofi

第六步. go test -a std

echo '# Testing packages.'

time go test std -short -timeout=$(expr 120 \* $timeout_scale)s

echo

接下来 run.bash 会在标准库里所有的包上来运行用 testing 包编写的单元测试。由于 $GOPATH 和 $GOROOT 中有着相同的命名空间,所以不能直接使用 go test … 否则 $GOPATH 中的每个包也会被逐一测试,因此创建了一个用于标准库中的包的别名:std。由于一些测试需要比较长的时间,且会消耗大量内存,因此用 -short 标志对一些测试进行了过滤。

第七步. runtime 和 cgo 测试

run.bash 接下来的部分会运行平台对 cgo 支持的测试,执行一些性能测试,并且编译一些伴随 Go 发行版一起的杂项程序。随着时间的流逝,这些杂项程序的清单会越来越长,那么它们也就会不可避免的被从编译过程中悄悄剥离出去。

第八步. go run test

(xcd ../test

unset GOMAXPROCS

time go run run.go

) || exit $?

run.bash 的倒数第二步会调用在 $GOROOT 下的 test 目录里的编译器和运行时的测试。他们是对于编译器和运行时自身的,较为低级细节的测试。会执行语言规格测试,test/bugs 和 test/fixedbugs 子目录保存有那些已经被发现并被修复的问题的独立的测试。驱动测试的是一个小 Go 程序 $GOROOT/test/run.go,会执行 test 目录里的每个 .go 文件。一些 .go 文件的首行包含了指导 run.go 对结果作出判断的指令,例如,程序将会失败,或提供一个确定的输出队列。

第九步. go tool api

echo '# Checking API compatibility.'

go tool api -c $GOROOT/api/go1.txt,$GOROOT/api/go1.1.txt \

-next $GOROOT/api/next.txt -except $GOROOT/api/except.txt

run.bash 的最后一步调用了 api 工具。

现代应用支持多平台运行是一件稀松平常的事情,在 Go 语言里面,为了支持应用的多平台部署,给用户提供了方便的配置方式来轻松构建针对不同操作系统和平台的运行文件。

Go 的构建约束,即构建标签,是以 // go:build 为开始的行注释,如果是 1.16 或之前的版本,格式是 // +build 。跟此变更相关的 issue 可以参考 25348 。

构建标签必须出现在 package 子句之前。为了区分构建标签和包文档的描述注释,构建标签后面应该有一个空行。

构建标签由||, &&, !运算符以及括号来组合表达。运算符与 Go 中的含义相同。

例如,以下构建标签在满足 linux 和 386 约束,或者满足 darwin 而 cgo 不满足时构建文件:

//go:build (linux &&386) || (darwin &&!cgo)

又如:仅在使用 cgo 时,且仅在 Linux 和 OS X 上构建文件: //go:build cgo &&(linux || darwin)

注意:1.17 及以后的表达格式里,一个文件有多个 //go:build 行是错误的。

在 1.16 及以前的版本,多行构建标签是允许的,并且组合方式是通过空格和逗号等来区分,空格符表示 OR,逗号表示 AND,感叹号表示 NOT。而多行之间则表示 OR。gofmt 命令将在遇到旧语法时添加等效的 //go:build 约束。如下是示例:

如果文件名在去除扩展名和可能的 _test 后缀后匹配以下任何模式, (例如:source_windows_amd64.go)其中 GOOS 和 GOARCH 分别代表任何已知的操作系统和体系结构值,那么认为该文件除了文件中的任何显式约束之外,具有这些术语的所表达的隐式构建标签。

除了官方提供的针对不同平台的内置标签,用户也可以使用自定义标签,例如 //go:build prod , 只需要在执行 go build 时显式带上标签名 go build --tags=prod 。

想要使文件构建时被忽略,可以使用: //go:build ignore ,其他任何没有被用来定义为标签的词也可以,但"ignore"是约定俗成的。)。Go 语言目前支持的系统和架构可以参考 官方文档 。