Install Go on macOS
This guide will help you install Go language on macOS system.
Download Go
First, download the Go installation package suitable for your macOS system from the official website.
Choose the corresponding version based on your system architecture:
darwin-amd64- Intel Mac (x86-64 architecture)darwin-arm64- Apple Silicon Mac (ARM64 architecture)
Installation Methods
Method 1: Using Installer Package (Recommended)
This is the simplest installation method, suitable for most users.
1. Download the installer
Download the .pkg installation package from the official website.
2. Run the installer
Double-click the downloaded .pkg file and follow the installer's prompts to complete the installation.
The installer will automatically:
- Install Go to
/usr/local/go(Intel Mac) or/opt/homebrew/opt/go(Apple Silicon Mac) - Configure the necessary environment variables
3. Verify installation
Open a new terminal window and execute:
go versionIf the installation is successful, you should see output similar to:
go version go1.21.3 darwin/arm64Method 2: Using Archive Package
This method is suitable for users who need to manage multiple Go versions or install to a custom path.
1. Download the archive package
Download the .tar.gz compressed package from the official website.
2. Delete old version (if any)
sudo rm -rf /usr/local/go3. Extract to /usr/local
sudo tar -C /usr/local -xzf go1.21.3.darwin-amd64.tar.gz4. Configure environment variables
Add the following to your ~/.zshrc or ~/.bash_profile file:
export PATH=$PATH:/usr/local/go/bin5. Make environment variables take effect
For zsh (default shell in macOS Catalina and later):
source ~/.zshrcFor bash:
source ~/.bash_profile6. Verify installation
go versionMethod 3: Using Homebrew
If you use Homebrew package manager, you can install Go through Homebrew.
1. Install Go
brew install go2. Verify installation
go version3. Upgrade Go
brew upgrade goCustom Installation Path
If you want to install Go to a custom path, you can:
1. Extract to custom directory
mkdir -p ~/go
tar -C ~/go -xzf go1.21.3.darwin-arm64.tar.gz2. Configure environment variables
Add the following to your ~/.zshrc or ~/.bash_profile file:
export GOROOT=$HOME/go
export PATH=$PATH:$GOROOT/bin3. Make environment variables take effect
source ~/.zshrcConfigure GOPATH
By default, Go uses $HOME/go as the workspace directory. If you want to use a different path, you can set the GOPATH environment variable:
export GOPATH=$HOME/gopath
export PATH=$PATH:$GOPATH/binVerify Installation
Create a simple Go program to verify that the installation is successful:
mkdir -p $HOME/hello
cd $HOME/hello
cat > hello.go << 'EOF'
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
EOF
go run hello.goIf the output is Hello, World!, it means the installation is successful.
Uninstall
Uninstall Installer Package Version
If you installed Go using the installer package, execute:
sudo rm -rf /usr/local/goOr for Apple Silicon Mac:
sudo rm -rf /opt/homebrew/opt/goUninstall Homebrew Version
If you installed Go using Homebrew, execute:
brew uninstall goUninstall Custom Path Version
If you installed Go to a custom path, just delete the corresponding directory:
rm -rf ~/goThen remove the Go-related environment variables from your ~/.zshrc or ~/.bash_profile file.
Troubleshooting
Command not found
If the go command is not found, please check:
- Whether the PATH environment variable is configured correctly
- Whether the environment variables have taken effect
- Try to restart the terminal or re-login
Version mismatch
If the displayed version does not match the installed version, please check:
- Whether there are multiple Go installations
- Whether the PATH environment variable points to the correct Go installation directory
- Execute
which goto view the actual path of the go command
Permission denied
If you encounter a permission denied error, please use sudo to execute the command:
sudo tar -C /usr/local -xzf go1.21.3.darwin-arm64.tar.gzNext Steps
After installation is complete, you can:
- Visit Go Getting Started Guide to learn Go language basics
- Visit Official Documentation for more detailed documentation
- Visit Go Tour to learn through interactive tutorials
