MacOS 使用Homebrew 安装go

18,903 阅读2分钟

1.Hmonebrew安装

homebrew官网, 移除brew

安装

# 这里不能使用root权限
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)" 

# 安装成功后检查brew版本
brew --version
Homebrew 2.2.14

# 安装成功,后面重点要把源 切到 aliyun、USTC中科大、tsinghua清华等国内源; 记住有问题先看官网!这种工具官网要是讲不清楚怎么用,会被吐槽死的~
#所以一定要先看官网

移除homebrew

当homebrew遇到问题时可以移除重装。 移除方法如下:

# 问题:homebrew安装时遇到问题,
Homebrew must be run under Ruby 2.6
# 先安装brew install Ruby@2.6 或者 rvm安装?
Updating Homebrew...
# 一直卡住了, 我感觉应该直接把brew卸载重装
# 根据官网的方法,移除brew
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall.sh)"
# 问题,这网址被墙了, 搜一个域名对应的ip 配置一下host可以了
curl: (7) Failed to connect to raw.githubusercontent.com port 443: Connection refused
# 重试
sudo /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall.sh)"

# 当重新安装brew时,情况会是这样的,brew会帮你搞定一切
➜  ~ brew --version
==> Downloading https://mirrors.ustc.edu.cn/homebrew-bottles/bottles-portable-ruby/portable-ruby-2.6.3.mavericks.bottle.tar.gz
###########################################                               60.4%

域名被墙了

配置好域名和ip映射,重试ok

➜  ~ sudo /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall.sh)"
Password:
Warning: This script will remove:
/Users/xxx/Library/Caches/Homebrew/
/Users/xxx/Library/Logs/Homebrew/
/usr/local/Cellar/
/usr/local/bin/brew -> /usr/local/bin/brew
Are you sure you want to uninstall Homebrew? This will remove your installed packages! [y/N] y
==> Removing Homebrew installation...
==> Removing empty directories...
==> /usr/bin/sudo /usr/bin/find /usr/local/bin /usr/local/etc /usr/local/include /usr/local/lib /usr/local/opt /usr/local/share /usr/local/var /usr/local/Homebrew -name .DS_Store -delete
==> /usr/bin/sudo /usr/bin/find /usr/local/bin /usr/local/etc /usr/local/include /usr/local/lib /usr/local/opt /usr/local/share /usr/local/var /usr/local/Homebrew -depth -type d -empty -exec rmdir {} ;
==> Homebrew uninstalled!
The following possible Homebrew files were not deleted:
/usr/local/.com.apple.installer.keep
/usr/local/Homebrew/
/usr/local/bin/
/usr/local/etc/
/usr/local/git/
/usr/local/include/
/usr/local/lib/
/usr/local/n/
/usr/local/opt/
/usr/local/remotedesktop/
/usr/local/share/
/usr/local/squid/
/usr/local/src/
/usr/local/var/
You may wish to remove them yourself.

raw.githubusercontent.com: -c: line 385: syntax error in conditional expression
raw.githubusercontent.com: -c: line 385: syntax error near `]]ping'
raw.githubusercontent.com: -c: line 385: `[[ $failed != true ]]ping'

切换国内源

我使用的是这个

cd "$(brew --repo)"
git remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-science"
git remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-science.git
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-python"
git remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-python.git

brew update

记得执行 source ~/.bash_profile

其他源的切换方法看这里,源切换方法,每种方法有点小区别。

查看源

brew config
HOMEBREW_VERSION: 2.2.14
# 设置成功
ORIGIN: https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git
HEAD: e2bc0fa6879ce4c74ea2d8f8c654c9812f776b43
Last commit: 5 days ago
Core tap ORIGIN: https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-python.git
Core tap HEAD: 1669af240605051cc3b0cf942ea918d22a49af27
Core tap last commit: 2 years, 4 months ago
HOMEBREW_PREFIX: /usr/local
# 这里有问题,可能是重装前的配置,需要编辑~/.zshrc文件改成 https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles
HOMEBREW_BOTTLE_DOMAIN: https://mirrors.ustc.edu.cn/homebrew-bottles
HOMEBREW_DISPLAY: /private/tmp/com.apple.launchd.GMuASfJSUg/org.macosforge.xquartz:0
HOMEBREW_MAKE_JOBS: 8
CPU: octa-core 64-bit haswell
# 看来brew是要依赖ruby
Homebrew Ruby: 2.6.3 => /usr/local/Homebrew/Library/Homebrew/vendor/portable-ruby/2.6.3/bin/ruby
Clang: 10.0 build 1001
Git: 2.6.2 => /usr/local/bin/git
Curl: 7.54.0 => /usr/bin/curl
Java: 1.8.0_40, 1.7.0_79
macOS: 10.14.6-x86_64
CLT: 10.3.0.0.1.1562985497
Xcode: 8.2.1
XQuartz: 2.7.7 => /opt/X11

遇到问题,重新配置完后验证一下,再次查看brew config ok。

这样 homebrew 基本就没问题了,后续使用brew安装软件会比较快。

2.Go安装

golang 官网需要翻下

问题排除

brew install go 失败,看来brew源有问题,

换一个中科大的试试,没问题了。

这个开始正确的,开始安装。

安装过程

www.jianshu.com/p/287fc9c7d…

HOMEBREW_BOTTLE_DOMAIN 貌似不是影响brew search,这个Formulae(库)各方应该都是一样的。

安装成功后,查看go版本

go version
go version go1.14.2 darwin/amd64

配置环境变量

open /usr/local/Cellar/go/
然后看一下自己的libexec在什么地方然后记录下整体的地址 我的地址是
/usr/local/Cellar/go/1.14.2_1/libexec

需要将这部分写入到 vim ~/.bash_profile

#GO
#GOROOT
export GOROOT=/usr/local/Cellar/go/1.14.2_1/libexec
#GOPATH
export GOPATH=$HOME/Documents/FEWoekSpace/golearn
#Bin
export PATH=${PATH}:$GOPATH/bin

好习惯, source ~/.bash_profile

验证

# 使用 go env 看看go环境是否安装成功
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/xxx/Library/Caches/go-build"
GOENV="/Users/xxx/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/xxx/Documents/FEWoekSpace/golearn"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/Cellar/go/1.14.2_1/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.14.2_1/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/1m/h306_y_s5sldc_318jjsndjr0000gn/T/go-build812986253=/tmp/go-build -gno-record-gcc-switches -fno-common"

hello world

赶紧试试go环境是否已经ok了...

创建helloworld.go 写入下面内容

package main  // 代码包声明语句。
import "fmt" //系统包用来输出的

func main() {
    // 打印函数调用语句。用于打印输出信息。
   fmt.Println("helloworld")
}

然后执行

go run helloworld.go

开始学习go吧