1.gin介绍

Gin是一个golang的微框架,封装比较优雅,API友好,源码注释比较明确,具有快速灵活,容错方便等特点.对于golang而言,web框架的依赖要远比Python,Java之类的要小。自身的net/http足够简单,性能也非常不错。
借助框架开发,不仅可以省去很多常用的封装带来的时间,也有助于团队的编码风格和形成规范

2. gin安装

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

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

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

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

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

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

import "net/http"

3、gin实现重定向

创建 Gin 文件夹,文件夹下创建 gin_redirect_test.go 文件;
gin_redirect_test.go 代码如下;

package Gin

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

// 重定向
func TestRedirect(t *testing.T) {
	r := gin.Default()
	r.GET("/index", func(c *gin.Context) {
		c.Redirect(http.StatusMovedPermanently,"http://www.baidu.com")
	})
	// Run("里面不指定端口号默认为8080") 
	r.Run(":8000")

	/*
	访问:http://localhost:8000/index ----> https://www.baidu.com/
	*/
}

浏览器访问 http://localhost:8000/index 地址:
在这里插入图片描述

可以看到重定向到了百度:
在这里插入图片描述
记录我的golang学习之路。。。