在CentOS上使用Golang進行開發,可以按照以下步驟進行:
更新系統:
sudo yum update -y
安裝EPEL倉庫(如果需要):
sudo yum install epel-release -y
使用yum安裝Golang(適用于CentOS 8及以上版本):
sudo yum install golang -y
手動下載并安裝Golang:
/usr/local
目錄下。wget https://golang.google.cn/dl/go1.23.3.linux-amd64.tar.gz
sudo tar -C /usr/local -xzf go1.23.3.linux-amd64.tar.gz
配置環境變量:
~/.bashrc
或~/.bash_profile
文件,添加以下內容:export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
export GO111MODULE=on
export GOPROXY=https://goproxy.cn,direct
source ~/.bashrc
驗證安裝:
go version
VS Code:
sudo yum install code -y
Ctrl+Shift+P
,輸入Go: Install/Update Tools
,選擇并安裝所需的工具。GoLand:
File -> Settings -> Go -> GOROOT
和GOPATH
,配置相應的路徑。創建一個簡單的Go程序(例如hello.go
):
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
編譯和運行程序:
go build hello.go
./hello
安裝調試工具:
go install github.com/go-delve/delve/cmd/dlv@latest
dlv version
配置Visual Studio Code進行調試:
launch.json
文件并配置調試功能:{
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${fileDirname}",
"env": {
"GO111MODULE": "on"
},
"args": []
}
]
}
dlv exec ./myapp
通過以上步驟,你就可以在CentOS上成功安裝并配置Golang開發環境,開始編寫和運行你的Go程序。