1 新建一个项目目录
mkdir wages_service
2 新建Makefile
文件
touch Makefile
文件内容如下
all: clean deps
clean:
@rm -rf go.mod
@rm -rf go.sum
@rm -rf vendor
deps:
@echo "初始化模块"
go mod init wages_service
@echo "下载依赖"
export GOPROXY=http://goproxy.io
go mod tidy -v
@echo "检查依赖是否全部下载"
go mod verify
@echo "创建vendor"
go mod vendor
3 新建main.go文件
package main
import (
"github.com/gin-gonic/gin"
"net/http"
"time"
)
func main() {
r := gin.Default()
r.GET("/test", func(c *gin.Context) {
data := map[string]interface{}{
"lang": "golang",
}
c.AsciiJSON(http.StatusOK, data)
})
s := &http.Server{
Addr: ":9000",
Handler: r,
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
MaxHeaderBytes: 1 << 20,
}
s.ListenAndServe()
}
4 执行命令
安装依赖包
make all
初始化模块
go mod init wages_service
go: creating new go.mod: module wages_service
初始化模块结束
下载依赖
export GOPROXY=http://goproxy.io
go mod tidy
go mod vendor
下载依赖结束
5 启动服务
go run main.go
[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.
[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
- using env: export GIN_MODE=release
- using code: gin.SetMode(gin.ReleaseMode)
[GIN-debug] GET /test --> main.main.func1 (3 handlers)
6 浏览器访问
http://localhost:9000/test
网友评论