Dtpark's blog 磨刀不误砍柴工,读完硕士再打工

Golang在Mac下的安装

2021-08-04

0x01 安装HomeBrew

HomeBrew是一款Mac OS平台下的软件包管理工具,拥有安装、卸载、更新、查看、搜索等很多实用的功能。简单的一条指令,就可以实现包管理,而不用你关心各种依赖和文件路径的情况,十分方便快捷。

  • 安装脚本(国内)
/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"
  • 卸载脚本(国内)
/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/HomebrewUninstall.sh)"
  • 参考资料

HomeBrew国内如何自动安装(国内地址)

0x02 安装Golang

2.1 首先看看有哪些golang版本可用

0x01@0x001 % brew search go
Warning: Error searching on GitHub: curl failed!   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
curl: (7) Failed to connect to api.github.com port 443: Connection refused

==> Formulae
algol68g                                 google-benchmark
anycable-go                              google-java-format
arangodb                                 google-sparsehash
argo                                     google-sql-tool
argocd                                   googler
argon2                                   googletest
aws-google-auth                          goolabs
bogofilter                               goose
cargo-c                                  gopass
cargo-instruments                        gor
certigo                                  goreleaser
cgoban                                   goreman
clingo                                   gost
django-completion                        gostatic
forego                                   gosu
fuego                                    gotags
gateway-go                               goto
git-hooks-go                             gotop
gnu-go                                   gource
go                                       govc
go-bindata                               govendor
go-jira                                  gowsdl
go-jsonnet                               gox
go-md2man                                gst-plugins-good
go-statik                                gx-go
go@1.10                                  hugo
go@1.11                                  jpegoptim
go@1.12                                  katago
go@1.13                                  lego
go@1.14                                 lgogdownloader
go@1.9                                 libgosu
goaccess                                 lunchy-go
goad                                     mongo-c-driver
gobby                                    mongo-cxx-driver
gobject-introspection                    mongo-orchestration
gobo                                     mongoose
gobuster                                 pango
gocloc                                   pangomm
gocr                                     powerline-go
gocryptfs                                protoc-gen-go
godep                                    protoc-gen-go-grpc
goenv                                    protoc-gen-gogo
gofabric8                                protoc-gen-gogofaster
goffice                                  pygobject3
gofish                                   ringojs
golang-migrate                           server-go
golangci-lint                            spaceinvaders-go
gollum                                   spigot
golo                                     svgo
gom                                      tengo
gomplate                                 wego
goocanvas                                wireguard-go
goofys                                   write-good
google-authenticator-libpam

我们发现最新的有1.14可以使用

2.2 安装brew下最新版本的go

0x01@0x001 % brew install go@1.14
Updating Homebrew...
==> Downloading https://homebrew.bintray.com/bottles/go@1.9-1.9.7.high_sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring go@1.14-1.14.10high_sierra.bottle.tar.gz
==> Caveats
A valid GOPATH is required to use the `go get` command.
If $GOPATH is not specified, $HOME/go will be used by default:
  https://golang.org/doc/code.html#GOPATH

You may wish to add the GOROOT-based install location to your PATH:
  export PATH=$PATH:/usr/local/opt/go@1.9/libexec/bin

This formula is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.

If you need to have this software first in your PATH run:
  echo 'export PATH="/usr/local/opt/go@1.14/bin:$PATH"' >> ~/.bash_profile

==> Summary
  /usr/local/Cellar/go@1.14/1.14.10: 7,668 files, 294.2MB

2.3 配置golang的相关环境变量

# MacOS 10.15 前的版本
0x01@0x001 % echo 'export PATH="/usr/local/opt/go@1.14/bin:$PATH"' >> ~/.bash_profile

# MacOS 10.15
0x01@0x001 % echo 'export PATH="/usr/local/opt/go@1.14/bin:$PATH"' >> ~/.zshrc

2.4 让改动生效

# MacOS 10.15 前的版本
0x01@0x001 % source ~/.bashrc

# MacOS 10.15
0x01@0x001 % source ~/.zshrc

2.5 测试是否安装成功

出现以下内容,则安装成功。

0x01@0x001 % go version
go version go1.14.10 darwin/amd64

0x03 参考资料

Mac os安装golang开发环境


上一篇 Hello,world!

Comments

Content