Skip to content
On this page

hello-world

项目初始化并启动

  • 初始化
shell
go mod init projectname

go mod init github.com/username/projectname
  • 下载gin
shell
go get -u github.com/gin-gonic/gin
  • 新建main.go
Go
package main

import (
	"net/http"

	"github.com/gin-gonic/gin"
)


func UserHandle(ctx *gin.Context) {
	ctx.String(http.StatusOK, "hello world")
} 

func main() {
	// 初始化路由
	router := gin.Default()
	router.GET("/user", UserHandle)
	// 启动http服务并监听
	router.Run(":9100")
}
  • 访问0.0.0.0:9100/user或者本机IP:9100/user查看结果