简介

1.1. 介绍

net/http

1.2. 安装

要安装Gin软件包,您需要安装Go并首先设置Go工作区。

1.首先需要安装Go(需要1.10+版本),然后可以使用下面的Go命令安装Gin。

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

进行下载的时候一般会下载失败,需要进行代理设置,参考以下博客进行设置即可

如果搭建过程中有问题,请了解go mod的使用 

2.将其导入您的代码中:

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

3.(可选)导入net/http。例如,如果使用常量,则需要这样做http.StatusOK。

import "net/http"

编写简单示例,验证gin框架是否搭建完成

package main

import (
    "net/http"

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

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")
}

 本地浏览器访问8000端口,输出hello world 验证完成