GoLang build gin project

1. Create the following three files in the directory

2. Configure the environment

 

GOPROXY=https://goproxy.io,direct

3. Download gin

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

4. Initialize the mod

go mod init example.com/m

5. Create main.go

package main

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

func main() {
	// 1.创建路由
	r := gin.Default()
	// 2.绑定路由规则,执行的函数
	// gin.Context,封装了request和response
	r.GET("/", func(c *gin.Context) {
		c.String(http.StatusOK, "hello World!")
	})
	// 3.监听端口,默认在8080
	// Run("里面不指定端口号默认为8080")
	r.Run(":8000")
}

6. Current directory structure

7. Run

go run main.go

8. Open the browser to visit

http://localhost:8080/

You're done

 

Guess you like

Origin blog.csdn.net/weixin_43101671/article/details/112966405