在Debian上运行Golang程序步骤如下:
安装Go环境
sudo apt update && sudo apt install golang-go
/usr/local
,配置环境变量(GOROOT
、GOPATH
、PATH
)并生效。go version
。编写Go程序
~/go/src/hello
),编写main.go
文件(示例代码:fmt.Println("Hello, World!")
)。运行程序
go run main.go
。go build -o hello
,再执行./hello
。后台运行(可选)
systemd
创建服务(需编写.service
文件,配置ExecStart
指向可执行文件路径,设置Restart=always
等参数),然后执行:sudo systemctl daemon-reload && sudo systemctl start hello
。其他部署方式(可选)
Dockerfile
(基于golang
镜像,复制代码并编译),构建镜像后运行容器。说明:Go 1.11+版本支持模块管理,可省略GOPATH
配置,直接通过go mod init
初始化项目。