创建开发用户和组
# 创建应用开发组groupadd -g 5000 appgroup# 创建开发用户useradd -g 5000 -u 5000 appuser# 设置用户密码passwd appuser << EOFappuserappuserEOF# 切换到 appuser 用户su - appuser# 在 appuser 用户下创建 IDE 目录mkdir -p ~/golang/ide
配置 Go 环境变量(appuser 用户下)
# 修改环境变量vim ~/.bash_profileexport GOROOT=/usr/local/goexport PATH=$GOROOT/bin:.:$PATH# 文件生效source ~/.bash_profile# 检查 Go 版本go version
安装配置 IDE 工具(GoLand)
# 上传 GoLand 工具到 /soft# 解压 GoLand 工具cd ~/golang/ide/tar -xzvf /soft/goland-2020.2.2.tar.gz# 改名解压文件mv GoLand-2020.2.2/ goland# 修改环境变量vim ~/.bash_profileexport GOROOT=/usr/local/goexport JAVA_HOME=/usr/local/jdk8export CLASSPATH=$JAVA_HOME/li:.export PATH=$JAVA_HOME/bin:/home/appuser/golang/ide/goland/bin:$GOROOT/bin:.:$PATHexport DISPLAY=:0
启动 GoLand
允许打开图行界面窗口(root 用户下)
xhost +
新建窗口,启动安装程序:goland.sh (appuser 用户下)





新建测试项目
创建项目工作空间
su - appusermkdir -p ~/golang/workspace
生成模块文件
cd ~/golang/workspace/demo1/go mod init demo1
创建项目 demo1

- 测试项目

使用 Gin 框架
webserver.go
package mainimport ("fmt""github.com/gin-gonic/gin""net/http")func main() {fmt.Println("web server")webserver := gin.Default()webserver.StaticFS("/public", http.Dir("html"))webserver.GET("/getUsers", func(context *gin.Context) {fmt.Println("web server1")// name := context.Query("name")// password := context.Query("password")name := context.Request.FormValue("userName")password := context.Request.FormValue("pass")fmt.Println("name:", name, "\tpassword:", password)context.JSON(200, gin.H{"user": "Jim",})})webserver.POST("/login", func(context *gin.Context) {fmt.Println("web server1")// name := context.Query("name")// password := context.Query("password")name := context.Request.PostFormValue("userName")password := context.Request.PostFormValue("pass")fmt.Println("name:", name, "\tpassword:", password)context.JSON(200, gin.H{"user": "Jim",})})webserver.Run(":9080")}
register.html

- 运行结果



使用 JSON 渲染
代码

运行结果

