go语言gin框架使用

go语言gin框架使用

安装gin

  • 下载gin框架

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

    报错:

    go get github.com/gin-gonic/gin: module github.com/gin-gonic/gin: Get "https://proxy.golang.org/github.com/gin-gonic/gin/@v/list": dial tcp 172.217.160.113:443: connectex: A connection attempt failed because the connected party di d not properly respond a
    

    报错原因需要配置代理服务器

    解决:

    go env -w GO111MODULE=on
    go env -w GOPROXY=https://goproxy.io,direct
    
  • import "github.com/gin-gonic/gin"
    
    package main
    
    import "github.com/gin-gonic/gin"
    
    func main() {
          
          
    	r := gin.Default()
    	r.GET("/", func(c *gin.Context) {
          
          
    		c.JSON(200, gin.H{
          
          
    			"message": "hello go !",
    		})
    	})
    	// r.Run() // 默认为localhost:8080
    	r.Run(":8089") // 修改为8089
    }
    

测试

访问http://localhost:8089/结果如下

在这里插入图片描述

学习来源

官方地址

猜你喜欢

转载自blog.csdn.net/succeedcow/article/details/116656230