当前位置:网站首页>GIN a preliminary study, the environment is installed

GIN a preliminary study, the environment is installed

2022-08-09 13:47:00 One Leaf Knows Autumn @qqy

前言

感谢开源项目gin-vue-admin,以及1010工作室的教程,项目文档
我只是在跟着学习,然后记录下笔记而已,可能会有新的代码加入,但是本质还是跟着学习的一个过程.

环境要求

Almost all are now in usemod管理包了,So it's definitely a requirementGO版本是1.1之上,本次直接使用GO1.16版本,That is, the latest1.16.6,goland采用2.21.2,Both use the latest version.
If there is an overseas link,So you can not set the following proxy.而且好像1.16Proxy is already used by default

在这里插入图片描述

go env -w GO111MODULE=on #设置全局开启 go mod Go1.16版本默认为on,可跳过这一步
go env -w GOPROXY=https://goproxy.cn,https://mirrors.aliyun.com/goproxy,direct #设置全局代理地址

创建项目

Create a project function folder in an external common place,然后打开goland.
点击File->New->Project
在这里插入图片描述

  • step1
    这里选择的GoThe implementation is used by defaultmod进行包管理,below herGo(gopath),It is the old way of package management,Not much to explain here,Because it has been gradually replaced by a way,Since it has been replaced, it means that there is something more powerful and convenient to replace,That is what is used nowmod.
  • step2
    这里的locationIn fact, it is the path where the new project is located
  • step3
    GOROOT很容易理解就是GO的安装路径.
    最后点击Create
    在这里插入图片描述
    After the generated interface is to contain only onemod文件的,Then create your own project file inside it.

安装GIN包

点击goland底部的Terminal打开终端,输入下面的命令进行安装:

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

在这里插入图片描述
-v 可以省略

下载postman

百度搜索postman,进入官网进行下载,下载地址
在这里插入图片描述
Choose according to your platform.
下载完后,A user registration is required to open the session,Use your usual email address to do it,Or log in with Google Mailok的,Then open the software and press it together when it is ready. It should be similar to the interface below.
在这里插入图片描述
点击Woekspaces->My Workspace
在这里插入图片描述
然后点击Create a request,It is almost the same as being able to enter the interface belowOK
在这里插入图片描述

创建示例代码

package main

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

func main() {
    
	r := gin.Default() //启动gin路由,携带基础中间件启动

	//Let the initiated route receiveget请求,且是/ping,Run an anonymous function,将gin的上下文传入
	r.GET("/ping", func(c *gin.Context) {
    
		c.JSON(200, gin.H{
     //Return the received information 回
			"message": "pong",
		})
	})
	r.Run() // listen and serve on 0.0.0.0:8080
}

在Goland中运行代码,然后再postman中进行验证
在这里插入图片描述
在这里插入图片描述
可以发现,再postmanThe result of the simulated request is the same as the one preset in the code

原网站

版权声明
本文为[One Leaf Knows Autumn @qqy]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/221/202208091244282328.html