Gin框架之环境搭建

一. 安装

首先确保得安装了go, 没安装go的可参考 Mac下环境搭建(用brew安装go和protoc)

~ go version
go version go1.17.2 darwin/amd64
go get -u github.com/gin-gonic/gin

二. 正常的go的helloworld

package main //1

import "fmt" //2

func main() {
    
     //3
   fmt.Println("Hello World") //4
}

三. gin框架下的go的helloworld

main.go中:

package main

import (
   "net/http"

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

func main() {
    
    
   r := gin.Default()
   r.GET("/gin", func(c *gin.Context) {
    
    
      c.String(http.StatusOK, "hello World!")
   })
   r.Run(":8000") // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}

然后执行go mod tidy安装依赖,最后浏览器输入
http://localhost:8000/gin可得到结果
在这里插入图片描述
在这里插入图片描述

参考链接:

  1. 2020最新Gin框架中文文档

猜你喜欢

转载自blog.csdn.net/weixin_45581597/article/details/127851745