GoLang搭建gin项目

2023年1月30日09:29:45

1.在目录中创建以下三个文件

2.配置环境

 

GOPROXY=https://goproxy.io,direct

3.下载gin

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

4.初始化mod

go mod init example.com/m

5.创建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.当前目录结构

7.运行

go run main.go

8.打开浏览器访问

http://localhost:8080/

大功告成

 

  • 作者:阿拉丁神灯_v
  • 原文链接:https://blog.csdn.net/weixin_43101671/article/details/112966405
    更新时间:2023年1月30日09:29:45 ,共 456 字。