windows 怎么编译 go语言

Python015

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 工具。

TinyGo是一个为微控制器、WebAssembly(Wasm)和命令行工具等小型场景设计的Go语言编译器。TinyGo重用了Go语言工具和LLVM使用的库,以编译用Go语言编写的程序。目前,该项目在GitHub上已经积累了10.1k的Star。

如下为一个示例程序,当运行在任何支持的带板载LED的主板上时,则会点亮内置LED。

上述程序可以在单片机、Adafruit ItsyBitsy M0微控制器或任何支持的带内置LED的板上进行编译和不需要修改的运行,只要设置正确的TinyGo编译器目标即可。例如,设置如下目标可以编译和点亮 单片机。

项目概述

TinyGo项目旨在将Go语言引入到具有单进程或核心的微控制器和小系统。TinyGo类似于emgo,但主要的区别在于作者想要保留Go内存模型。另一个区别在于TinyGo在内部使用LLVM,因而可以获得更小更高效的代码以及更高的灵活性。

创建TinyGo项目的初衷是,如果Python可以在微控制器上运行,Go语言当然也应该能够在更低级微设备上运行。

支持设备

你可以为微控制器、WebAssembly和Linux编译TinyGo程序。目前,TinyGo支持以下85种微处理器板。

更多技术细节请参阅原项目。