Gin框架基础配置

 预备工作

1. 下载并安装 gin:

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

2. 将 gin 引入到代码中:

import "github.com/gin-gonic/gin

3.运行指令

go mod tidy 

4.代码开发时需要不断的修改代码,然后重新编译启动程序,需要重复性的执行相关命令,如果使用 air工具,就可以自动处理这个过程。

go install github.com/cosmtrek/air

安装完成后我的air目录为:

E:\Go_Workplace\src\pkg\mod\github.com\cosmtrek\air@v1.47.0

接下来Windows环境需要配置环境变量 需要.exe文件,在这个目录下cmd 执行

go build .

 接下来在环境变量中添加这个目录

E:\Go_Workplace\src\pkg\mod\github.com\cosmtrek\air@v1.47.0

重启cmd窗口,执行

air -v

 

 5.在公更目录下配置air.conf文件(我选择配置在这里)

在这个文件中,将下面内容粘贴进去

# [Air](https://github.com/cosmtrek/air) TOML 格式的配置文件
 
# 工作目录
# 使用 . 或绝对路径,请注意 `tmp_dir` 目录必须在 `root` 目录下
root = "."
tmp_dir = "tmp"
 
[build]
# 只需要写你平常编译使用的shell命令。你也可以使用 `make`
# Windows平台示例: cmd = "go build -o ./tmp/main.exe ."
cmd = "go build -o ./tmp/main.exe ."
# 由`cmd`命令得到的二进制文件名
# Windows平台示例:bin = "tmp/main.exe"
bin = "tmp/main.exe"
# 自定义执行程序的命令,可以添加额外的编译标识例如添加 GIN_MODE=release
# Windows平台示例:full_bin = "./tmp/main.exe"
# Linux平台示例:full_bin = "APP_ENV=dev APP_USER=air ./tmp/main.exe"
full_bin = "./tmp/main.exe"
# 监听以下文件扩展名的文件.
include_ext = ["go", "tpl", "tmpl", "html"]
# 忽略这些文件扩展名或目录
exclude_dir = ["assets", "tmp", "vendor", "frontend/node_modules"]
# 监听以下指定目录的文件
include_dir = []
# 排除以下文件
exclude_file = []
# 如果文件更改过于频繁,则没有必要在每次更改时都触发构建。可以设置触发构建的延迟时间
delay = 1000 # ms
# 发生构建错误时,停止运行旧的二进制文件。
stop_on_error = true
# air的日志文件名,该日志文件放置在你的`tmp_dir`中
log = "air_errors.log"
 
[log]
# 显示日志时间
time = true
 
[color]
# 自定义每个部分显示的颜色。如果找不到颜色,使用原始的应用程序日志。
main = "magenta"
watcher = "cyan"
build = "yellow"
runner = "green"
 
[misc]
# 退出时删除tmp目录
clean_on_exit = true 

 在你的项目中执行

air

一.第一个gin程序

package main

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

// GET 请求
func getkey(c *gin.Context) {
	s := c.Query("name")
	age := c.DefaultQuery("age", "xxoo")
	c.String(200, "this is getkey()获取到key=%s  %s", s, age)
}

// POST 请求
func posrVal(c *gin.Context) {
	s := c.PostForm("name")
	age := c.DefaultPostForm("age", "xxoo")
	c.String(200, "this is postVal()获取到key=%s  %s", s, age)
}
func main() {
	//创建一个默认路由
	r := gin.Default()
	//配置路由
	r.GET("ping", func(c *gin.Context) {
		c.JSON(200, gin.H{ //返回JSON格式数据
			"msg": "pong",
		})
	})
	r.POST("/postVal", posrVal)
	//r.GET("/someGet", someGet)
	r.GET("/getkey", getkey)
	r.Run()
}

 看到这里,我们的路刚刚开始!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值