go学习笔记:安装并运行hello world

  • homebrew安装go
  • 配置环境变量
  • 在终端运行go程序

通过homebrew安装go

1
2
brew update
brew install go

查看安装的路径:

添加环境变量

编辑~/.zshrc

1
2
3
export GOROOT=/usr/local/opt/go/libexec
export GOPATH=$HOME/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin

保存后执行

1
source ~/.zshrc

在终端环境运行go程序

新建文件hello.go,编辑如下并保存

~/code/go/src/hello.go
1
2
3
4
5
6
7
// hello.go
package main
import "fmt"
func main() {
fmt.Printf("Hello, world!")
}

go run

在终端执行

1
2
cd ~/code/go/src/hello.go
go run hello.go

go build

也可以先用build命令编译,再执行

1
2
go build hello.go //生成可执行文件hello
./hello

go tool objdump 查看执行过程

https://golang.org/cmd/objdump/
Objdump prints a disassembly of all text symbols (code) in the binary. If the -s option is present, objdump only disassembles symbols with names matching the regular expression.

1
2
go build hello.go
go tool objdump -s "main\.main" hello

参考

https://golang.org/doc/install?download=go1.8.1.darwin-amd64.pkg