Install using Homebrew

To ensure that Homebrew is installed, run brew Install go

Homebrew will automatically install the current stable version and wait a few minutes for the installation to complete

cd ~ 
vim .bash_profile
Copy the code

Configuring the path involves copying and pasting the following into the bash_profile file

export GOROOT=/usr/local/opt/go/libexec // Homebrew install Go directoryexportGOPATH=/Users/liMacbookPro/go // Create a go folder as a working directoryexport GOBIN=$GOPATH/bin
export PATH=$PATH:$GOROOT/bin:$GOBIN
Copy the code
  • GOROOT specifies the go language SDK installation directory
  • PATH Bin directory under the SDK
  • The working directory for the GOPATH project

Then execute source ~/.bash_profile for the environment variable to take effect

The final step, go env, appears as follows, and the installation is successful

GOBIN="" GOCACHE="/Users/liMacBookPro/Library/Caches/go-build" GOEXE="" GOFLAGS="" GOHOSTARCH="amd64" GOHOSTOS="darwin" GOOS = "Darwin" GOPATH = "/ Users/lihu/go" GOPROXY = "" GORACE =" "GOROOT ="/usr/local/Cellar/go / 1.11.5 / libexec "GOTMPDIR =" " GOTOOLDIR = "/ usr/local/Cellar/go / 1.11.5 / libexec/PKG/tool/darwin_amd64" GCCGO = "GCCGO" 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/kb/5jyxjnc95_1bt92wfkgbnpm80000gn/T/go-build127386950=/tmp/go-build -gno-record-gcc-switches -fno-common"Copy the code

Create a new hello.go file to test the input

package main
import "fmt"

func main(){
  fmt.Printf("hello")}Copy the code

Then, in the current directory, open the terminal and run go Run hello.go, or you can use go Build to compile the executable binary file and run the./hello execution binary file