以下是在Ubuntu中搭建Golang开发环境的步骤:
sudo apt update和sudo apt install golang-go,安装完成后用go version验证。wget https://golang.org/dl/go1.21.5.linux-amd64.tar.gz,然后解压到/usr/local目录,执行sudo tar -C /usr/local -xzf go1.21.5.linux-amd64.tar.gz,并清理下载文件。sudo apt update和sudo apt install snapd,再安装Go,sudo snap install go --classic,最后用go version验证。手动安装时需配置,编辑~/.bashrc或~/.zshrc文件,添加export GOROOT=/usr/local/go、export GOPATH=$HOME/go、export PATH=$GOROOT/bin:$GOPATH/bin:$PATH,然后执行source ~/.bashrc或source ~/.zshrc使配置生效。
可安装Git、gofmt、golint等工具,如sudo apt install git,以及go install golang.org/x/tools/cmd/goimports@latest等。
创建项目目录,如mkdir -p $HOME/go/src/hello-world,进入目录后初始化模块go mod init hello-world,创建main.go文件并写入代码,最后用go run main.go运行程序。