Go源码编译

Building Go from Source

原文作者:Carolyn Van Slyck This is a part of a larger effort that I’m calling GopherSource, and our goal is for more of the gopher community to become upstream Go contributors.

This is a part of a larger effort that I’m calling GopherSource, and our goal is for more of the gopher community to become upstream Go contributors.

Before we can become Go contributors, the very first step is being able to build Go from source. I followed the official doc and filled in the blanks a bit to figure out how to get everything working.

Go is compiled with … Go! ? Since I already have Go 1.10 installed on my computer, I decided to use that. Another option is to bootstrap Go, Karen Carcamo wrote up how to do that on Linux.

Here is the tldr of how to download the Go source and build it:

1. Clone the Go repository, it doesn’t have to be in your GOPATH.

1 git clone https://go.googlesource.com/go

If you want to build a specific version of Go, now’s the time to checkout that version using a tag, for example git checkout go1.10.3. Since we want to try out new features, namely Go Modules (the prototype formerly known as vgo), I am going to use the latest changes on the master branch. If you just cloned, you can skip this step since you already have the latest changes.

1 git checkout master
2 git pull

2. Compile Go! If you are on Windows, use make.bat instead.

 1 $ cd src
 2 $ ./make.bash
 3 Building Go cmd/dist using /usr/local/Cellar/go/1.10.3/libexec.
 4 Building Go toolchain1 using /usr/local/Cellar/go/1.10.3/libexec.
 5 Building Go bootstrap cmd/go (go_bootstrap) using Go toolchain1.
 6 Building Go toolchain2 using go_bootstrap and Go toolchain1.
 7 Building Go toolchain3 using go_bootstrap and Go toolchain2.
 8 Building packages and commands for darwin/amd64.
 9 ---
10 Installed Go for darwin/amd64 in /Users/carolynvs/src/go
11 Installed commands in /Users/carolynvs/src/go/bin

3. Open a new shell session. Now prepend the bin directory from the output above to your PATH so that you are using your custom go binary by default.

1 export PATH=/Users/carolynvs/src/go/bin:$PATH

On Windows use this command instead:

1 $env:PATH = "/Users/carolynvs/src/go/bin;"+$env:PATH

Make sure to replace /Users/carolynvs/src/go with the location where you cloned the Go repository.

4. Use the version command to verify that you are now using your custom build of Go.

1 $ go version
2 go version devel +a12c1f26e4 Tue Jun 26 20:00:51 2018 +0000 darwin/amd64

Now that we have the latest version of Go, let’s try it out and use a new feature that has been added to Go 1.11, Go Modules! Go Modules is the official name going forward for the vgo prototype and is the next step in the evolution of dependency management in Go.

Luckily, you don’t need to know what that is in order to see it in action. Now when you run go build, Go will take care of retrieving your dependencies, not just master but even a specific version or commit! ?

1. Clone my test repository, ANYWHERE YOU LIKE! Doesn’t have to be in the GOPATH.

1 git clone https://github.com/carolynvs/go-depmgmt-testrepo

2. My repo relies upon external code that isn’t checked into the vendor directory. When you run the code, Go retrieves v0.8.0 of the errors package before building.

1 $ go run main.go
2 go: finding github.com/pkg/errors v0.8.0
3 go: downloading github.com/pkg/errors v0.8.0
4 hello world!

There are some extra debug lines printed in the output, those are from the new Go modules feature:

1go: finding github.com/pkg/errors v0.8.0
2go: downloading github.com/pkg/errors v0.8.0

3. Try running the code again, and you won’t see those extra lines.

1 $ go run main.go
2 hello world!

Go downloaded the dependency and cached it in $GOPATH/src/mod. Next time you build, Go will retrieve that package from its cache.

 1 $ tree ~/go/src/mod
 2 ~/go/src/mod
 3 ├── cache
 4 │   ├── download
 5 │   │   └── github.com
 6 │   │       └── pkg
 7 │   │           └── errors
 8 │   │               └── @v
 9 │   │                   ├── list
10 │   │                   ├── v0.8.0.info
11 │   │                   ├── v0.8.0.mod
12 │   │                   ├── v0.8.0.zip
13 │   │                   └── v0.8.0.ziphash
14 └── github.com
15     └── pkg
16         └── errors@v0.8.0
17             ├── LICENSE
18             ├── README.md
19             ├── appveyor.yml
20             ├── bench_test.go
21             ├── errors.go
22             ├── errors_test.go
23             ├── example_test.go
24             ├── format_test.go
25             ├── stack.go
26             └── stack_test.go

We will go into more detail about Go modules and what’s happening under the covers in the next blog post!

? Step 1 of becoming a real Go contributor accomplished! ?

If you’d like to play with Go Modules more, a bunch of friendly gophers are sharing what we learn as we go along and you can join us in the Go forums in the Exploring Go Modules thread. Otherwise if you want all the info but don’t have time to play, I will do a followup post in a few days summarizing what we discover.

版权申明:内容来源网络,版权归原创者所有。除非无法确认,我们都会标明作者及出处,如有侵权烦请告知,我们会立即删除并表示歉意。谢谢。

本文分享自微信公众号 - Golang语言社区(Golangweb)

原文出处及转载信息见文内详细说明,如有侵权,请联系 yunjia_community@tencent.com 删除。

原始发表时间:2018-07-22

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • google舍弃java?android上层应用或采用Go语言

    摘要 Go语言是谷歌2009发布的第二款开源编程语言。Go语言专门针对多处理器系统应用程序的编程进行了优化,使用Go编译的程序可以媲美C或C++代码的速度,而且...

    李海彬
  • 【文末有惊喜】初探Go的编译命令执行过程

    引言 ? Go 语言这两年在语言排行榜上的上升势头非常猛,Go 语言虽然是静态编译型语言,但是它却拥有脚本化的语法,支持多种编程范式(函数式和面向对象)。Go ...

    李海彬
  • Go微服务 - 构建我们的第一个服务

    虽然通过HTTP提供JSON服务不是内部服务和外部服务的唯一选择,但本文聚焦的是HTTP和JSON. 使用RPC机制和二进制消息格式(例如Protocol Bu...

    李海彬
  • golang下载安装

    官方二进制发行版 支持 FreeBSD(8-STABLE 发行版及以上)、Linux、Mac OS X(Snow Leopard 及以上)和 Windows 操...

    超蛋lhy
  • Go 标准库

    Go标准库之强大好用,用过的人都知道。在说标准库之前,可能要先说一下Go语言里“包”的概念,一个包代表了Go语言中代码组织的主要形式,因为Go对单文件没有限制,...

    icepy
  • go语言学习初体验

    我们暂时先抛开go语言,咱们说下行业里近两年的哪些技术发展最火,毫无疑问是Docker和Kubernetes,没错,他们是go语言的顶级项目。

    jeanron100
  • SQL JOIN

    在机房收费系统个人重构的时候,很多的功能都需要根据数据库中的一个表中的信息,去查询另一个表中相匹配的信息,我们用到了视图,但是你有没有注意到下面的SQL语句呢...

    令仔很忙
  • Python3网络爬虫实战-19、代理基

    我们在做爬虫的过程中经常会遇到这样的情况,最初爬虫正常运行,正常抓取数据,一切看起来都是那么的美好,然而一杯茶的功夫可能就会出现错误,比如 403 Forbid...

    py3study
  • 当redis宕机重启以后,该节点不能跟选举后的master数据主从同步

    当redis以集群方式进行部署,此时,master宕机了,由哨兵机制选举新的newMaster出来。 当oldmaster重启以后,会变成slave节点

    envoke
  • SQL(结构化查询语言)注入

    SQL注入(也称为SQLI)是一种常见的攻击媒介,它使用恶意SQL代码用于后端数据库操作,以访问不打算显示的信息。此信息可能包括任何数量的项目,包括敏感的公司数...

    周俊辉

扫码关注云+社区

领取腾讯云代金券