go语言 使用Gin框架写一个简单的web服务器

mac2026-05-11  1

安装 go 语言环境

安装 Gin 包

go get -u github.com/gin-gonic/gin

新建文件 webhttp.go

package main import "github.com/gin-gonic/gin" func main() { r := gin.Default() r.GET("/ping", func(c *gin.Context) { c.JSON(200, gin.H{ "message": "pong", }) }) r.Run() // listen and serve on 0.0.0.0:8080 }

运行项目

go run webhttp.go

访问http://localhost:8080/ping

出现 {"message":"pong"}

运行成功.

也可以将文件打包成exe

go build webhttp.go

生成webhttp.exe ,双击可以运行

 

最新回复(0)