创建开发用户和组
# 创建应用开发组
groupadd -g 5000 appgroup
# 创建开发用户
useradd -g 5000 -u 5000 appuser
# 设置用户密码
passwd appuser << EOF
appuser
appuser
EOF
# 切换到 appuser 用户
su - appuser
# 在 appuser 用户下创建 IDE 目录
mkdir -p ~/golang/ide
配置 Go 环境变量(appuser 用户下)
# 修改环境变量
vim ~/.bash_profile
export GOROOT=/usr/local/go
export 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_profile
export GOROOT=/usr/local/go
export JAVA_HOME=/usr/local/jdk8
export CLASSPATH=$JAVA_HOME/li:.
export PATH=$JAVA_HOME/bin:/home/appuser/golang/ide/goland/bin:$GOROOT/bin:.:$PATH
export DISPLAY=:0
启动 GoLand
允许打开图行界面窗口(root 用户下)
xhost +
新建窗口,启动安装程序:goland.sh
(appuser 用户下)
新建测试项目
创建项目工作空间
su - appuser
mkdir -p ~/golang/workspace
生成模块文件
cd ~/golang/workspace/demo1/
go mod init demo1
创建项目 demo1
- 测试项目
使用 Gin 框架
webserver.go
package main
import (
"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 渲染
代码
运行结果