【GO教程5】Mac搭建简单Gin项目
-官方文档:https://github.com/gin-gonic/gin# 搭建go环境:请参考:【GO教程-3】Mac搭建go环境https://blog.csdn.net/weixin_41827162/article/details/115612993如果不想使用以上搭建go的教程,则可用下面教程:1. Mac直接安装go安装包:https://golang.google.cn/dl/g
-
官方文档:https://github.com/gin-gonic/gin
# 搭建go环境:
请参考:【GO教程-3】Mac搭建go环境 https://blog.csdn.net/weixin_41827162/article/details/115612993
如果不想使用以上搭建go的教程,则可用下面教程:
1. Mac直接安装go安装包:
https://golang.google.cn/dl/go1.16.3.darwin-amd64.pkg
2. 写入自定义参数:
进入Mac用户根目录:
cd ~
创建 .bash_profile:
touch .bash_profile
打开.bash_profile:
open .bash_profile
然后写入如下内容:
export GOROOT=/usr/local/go
export GOPATH=/Users/fyonecon/go
export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOROOT/bin:$GOBIN
export GO111MODULE=on
export GOPROXY=https://goproxy.cn,direct
直接在用户根目录编译上面文件:
source ~/.bash_profile
# 搭建简单的Gin环境
1. 获取gin框架:
(当前目录为cd ~后的目录)
go get -u github.com/gin-gonic/gin
2. 新建一个gin项目启动文件:
新建/Users/fyonecon/go/src/gindemo1/ 文件夹(在$GOPATH/src/目录中),
在/gindemo1/文件夹中新建main.go文件,并填入下面内容:
package main
import "github.com/gin-gonic/gin"
import _ "net/http"
func main() {
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
})
r.Run(":8080")
}
3. 初始化go.mod:
go mod init
4. 初始化go.sum:
go build -mod=mod
5. 启动此gin项目:
go run main.go
成功访问http://127.0.0.1:8080/ping
-
更多推荐
所有评论(0)