栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Go语言

Gin框架学习--搭建gin框架,并运行起来

Go语言 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Gin框架学习--搭建gin框架,并运行起来

安装go

我是windows开发环境。下载golang安装包:https://studygolang.com/dl/golang/go1.15.6.windows-amd64.msi
一般是会自动添加环境变量的,无需修改;

设置国内包源:
go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,direct  #这样的话下载包很快
go env
安装gin框架

框架地址:https://github.com/gin-gonic/gin
在你要新建项目的地方运行以下命令:

go mod init myapp #这个myapp是项目名

执行完会提示:

go: creating new go.mod: module myapp 

然后我们就可以安装你想要的包了:

go get github.com/gin-gonic/gin@latest
或者:go mod download github.com/gin-gonic/gin@latest

查看go mod帮助:

PS D:LearnGogin> go help mod
Go mod provides access to operations on modules.

Note that support for modules is built into all the go commands,
not just 'go mod'. For example, day-to-day adding, removing, upgrading,
and downgrading of dependencies should be done using 'go get'.
See 'go help modules' for an overview of module functionality.

Usage:

        go mod  [arguments]

The commands are:

        download    download modules to local cache
        edit        edit go.mod from tools or scripts
        graph       print module requirement graph
        init        initialize new module in current directory
        tidy        add missing and remove unused modules
        vendor      make vendored copy of dependencies
        verify      verify dependencies have expected content
        why         explain why packages or modules are needed

Use "go help mod " for more information about a command.

测试gin是否正常安装

在任意位置打开命令行,运行以下命令:

gin.exe  -h

如下图所示即正确安装:

第一个程序,跑起来

在你项目新建一个文件main.go,内容如下:

package main

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

func main() {
	r := gin.Default()
	r.GET("/ping", func(c *gin.Context) {
		c.JSON(200, gin.H{
			"message": "Bruce",
		})
	})
	r.Run()
}
gin.exe run main.go  

访问浏览器:

注意:默认是808端口,我这个端口是我配置热重启之后程序更换的。不安装热重启的话每次都需要重新运行程序,比较麻烦,所以建议立马装上热重启,加快开发速度。

配置热重启:

需要下载一个包:

go get github.com/codegangsta/gin
gin.exe run main.go  

比较顺利,框架已经跑起来了。有时间再写个简单的curd。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/945079.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号