“Gin”的版本间的差异
跳到导航
跳到搜索
() |
(→learn) |
||
(未显示同一用户的54个中间版本) | |||
第1行: | 第1行: | ||
+ | [[category:go]] | ||
=install= | =install= | ||
+ | https://github.com/goproxy/goproxy.cn | ||
==pre install golang== | ==pre install golang== | ||
[[Golang]] | [[Golang]] | ||
第9行: | 第11行: | ||
sudo apt install golang-go gccgo-go | sudo apt install golang-go gccgo-go | ||
sudo apt install golang-go | sudo apt install golang-go | ||
+ | </pre> | ||
+ | |||
+ | ==FQ== | ||
+ | <pre> | ||
+ | tail /etc/profile | ||
+ | #export http_proxy=http://192.168.10.173:8888 | ||
+ | #export https_proxy=http://192.168.10.173:8888 | ||
+ | |||
+ | |||
+ | source /etc/profile | ||
</pre> | </pre> | ||
== install Gin package== | == install Gin package== | ||
− | go get -u github.com/gin-gonic/gin | + | export PATH=/usr/local/go/bin/:$PATH |
+ | export GOPATH=/root/go #if U user is root | ||
+ | === If you're in China=== | ||
+ | ====如果您使用的 Go 版本是 1.13 及以上 ==== | ||
+ | <pre> | ||
+ | #nov 29 2021 | ||
+ | export GO111MODULE=on | ||
+ | export GOPROXY=https://goproxy.cn | ||
+ | |||
+ | |||
+ | or | ||
+ | |||
+ | echo "export GO111MODULE=on" >> ~/.profile | ||
+ | echo "export GOPROXY=https://goproxy.cn" >> ~/.profile | ||
+ | source ~/.profile | ||
+ | |||
+ | |||
+ | |||
+ | go env -w GO111MODULE=on #注意这个打开后面可能会问题导致 | ||
+ | go env -w GOPROXY=https://goproxy.io,direct | ||
+ | |||
+ | #or | ||
+ | go env -w GOPROXY=https://mirrors.aliyun.com/goproxy/,direct | ||
+ | |||
+ | # 设置不走 proxy 的私有仓库,多个用逗号相隔(可选) | ||
+ | go env -w GOPRIVATE=*.corp.example.com | ||
+ | |||
+ | # 设置不走 proxy 的私有组织(可选) | ||
+ | go env -w GOPRIVATE=example.com/org_name | ||
+ | </pre> | ||
+ | |||
+ | ====Go 版本是 1.12 及以下 ==== | ||
+ | <pre> | ||
+ | # 启用 Go Modules 功能 | ||
+ | export GO111MODULE=on | ||
+ | # 配置 GOPROXY 环境变量 | ||
+ | export GOPROXY=https://goproxy.io | ||
+ | </pre> | ||
+ | |||
+ | 或者,根据[https://goproxy.io/zh/docs/getting-started.html 文档]可以把上面的命令写到.profile或.bash_profile文件中长期生效 | ||
+ | |||
+ | =初始化 gin= | ||
+ | ==go mod== | ||
+ | <pre> | ||
+ | go env -w GO111MODULE=on #注意这个打开后面可能会问题导致 | ||
+ | go env -w GOPROXY=https://goproxy.io,direct | ||
+ | |||
+ | cd mygo/ | ||
+ | mkdir ginweb | ||
+ | cd ginweb/ | ||
+ | go mod init ginweb(名字自定义) | ||
+ | go get -u github.com/gin-gonic/gin #注意 ,用go mod 来管理 june 08 2020 goverdor 已过期 请用 go mod 安装一般用下面的goverdor 版本管理 tool 去安装 这一块可以直接跳过 | ||
+ | go run example.go | ||
+ | |||
+ | ps | ||
+ | evan@myxps:~/mygo/ginweb$ echo $GOROOT | ||
+ | /home/evan/go | ||
+ | evan@myxps:~/mygo/ginweb$ echo $GOPATH | ||
+ | /home/evan/go | ||
+ | |||
+ | #别人的例子 | ||
+ | 编写 HelloWorld 文件,测试运行环境。 | ||
+ | |||
+ | package main | ||
+ | import "fmt" | ||
+ | func main(){ | ||
+ | fmt.Println("hello,world!") | ||
+ | } | ||
+ | |||
+ | go run hello.go | ||
+ | go build hello.go | ||
+ | |||
+ | 创建 go.mod 文件 | ||
+ | |||
+ | go mod init hello | ||
+ | |||
+ | 用 Gin 实现一个简单的 http 服务 | ||
+ | |||
+ | import ( | ||
+ | "gopkg.in/gin-gonic/gin.v1" | ||
+ | "net/http" | ||
+ | ) | ||
+ | |||
+ | func main(){ | ||
+ | |||
+ | router := gin.Default() | ||
+ | |||
+ | router.GET("/", func(c *gin.Context) { | ||
+ | c.String(http.StatusOK, "Hello World") | ||
+ | }) | ||
+ | router.Run(":8000") | ||
+ | } | ||
+ | |||
+ | 直接编译执行 | ||
+ | |||
+ | go run hello | ||
+ | |||
+ | 可以看到引用的包都被自动下载了 | ||
+ | </pre> | ||
+ | |||
+ | == 放弃了 Use a vendor tool like Govendor== | ||
+ | go get -u -v github.com/kardianos/govendor | ||
+ | go get github.com/kardianos/govendor #在墙外非常快 | ||
+ | |||
+ | sudo apt install govendor # apt | ||
+ | |||
+ | mkdir -p $GOPATH/src/github.com/myusername/project && cd "$_" | ||
+ | |||
+ | ===Vendor init your project and add gin === | ||
+ | <pre> | ||
+ | govendor init | ||
+ | govendor fetch github.com/gin-gonic/[email protected] | ||
+ | |||
+ | #有时要绝对路径 @后面也可以不加 有时一定要加 | ||
+ | /root/go/bin/govendor fetch github.com/gin-gonic/[email protected] | ||
+ | </pre> | ||
+ | |||
+ | =learn= | ||
+ | go官方推出的gin教程 | ||
+ | https://docs.studygolang.com/doc/tutorial/web-service-gin | ||
+ | |||
+ | https://go.dev/doc/tutorial/web-service-gin | ||
+ | |||
+ | |||
+ | |||
+ | https://github.com/astaxie/build-web-application-with-golang | ||
+ | |||
+ | https://github.com/avelino/awesome-go | ||
+ | |||
+ | https://gin-gonic.com/docs/quickstart/ | ||
+ | |||
+ | https://github.com/gin-gonic/examples | ||
+ | |||
+ | [https://geektutu.com/post/quick-go-gin.html Go Gin 简明教程 ] | ||
+ | |||
+ | |||
+ | https://learnku.com/docs/gin-gonic/2019 | ||
+ | |||
+ | [https://www.jianshu.com/p/c0711568660c GO语言web框架Gin之完全指南(一)] | ||
+ | |||
+ | [https://www.jianshu.com/p/ebd4413bab73 GO语言web框架Gin之完全指南(二] | ||
+ | |||
+ | [https://www.cnblogs.com/ningskyer/articles/6297356.html 初学Golang:Gin 框架中文文档教程 ] | ||
+ | |||
+ | |||
+ | [https://www.jianshu.com/p/2a1a74ad3c3a Gin middleware中间件使用实例] | ||
+ | |||
+ | [https://blog.csdn.net/breaksoftware/article/details/84667656 Gin源码解析和例子——路由] | ||
+ | |||
+ | https://godoc.org/golang.org/x/tools/cmd | ||
+ | |||
+ | =IDE= | ||
+ | |||
+ | [https://www.jianshu.com/p/c5368dbd8bf9 golnad and 50.Go Mod 来创建 Gin 项目] | ||
+ | |||
+ | =trouble= | ||
+ | == working directory is not part of a module== | ||
+ | <pre> | ||
+ | |||
+ | 关闭go代理就行了 或者 google | ||
+ | |||
+ | ct$ go run main.go | ||
+ | main.go:6:2: cannot find module providing package github.com/gin-gonic/gin: working directory is not part of a module | ||
+ | evan@myxps:~/go/src/github.com/myusername/project$ go env -w GO111MODULE=off | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | go run 2.go | ||
+ | 2.go:3:8: cannot find module providing package github.com/gin-gonic/gin: working directory is not part of a module | ||
+ | evan@myxps:~/data/devops/go/tmp$ echo $GOROOT | ||
+ | |||
+ | evan@myxps:~/data/devops/go/tmp$ go mod init gin | ||
+ | go: creating new go.mod: module gin | ||
+ | evan@myxps:~/data/devops/go/tmp$ ls | ||
+ | 1.go 2.go go.mod | ||
+ | evan@myxps:~/data/devops/go/tmp$ go mod vendor | ||
+ | go: finding module for package github.com/gin-gonic/gin | ||
+ | go: found github.com/gin-gonic/gin in github.com/gin-gonic/gin v1.7.7 | ||
+ | </pre> | ||
+ | |||
+ | ==1.6 err== | ||
+ | <pre> | ||
+ | # go run main.go | ||
+ | # github.com/myusername/project/vendor/github.com/gin-gonic/gin | ||
+ | vendor/github.com/gin-gonic/gin/context.go:36:26: undefined: binding.MIMEYAML | ||
+ | vendor/github.com/gin-gonic/gin/context.go:600:29: undefined: binding.YAML | ||
+ | vendor/github.com/gin-gonic/gin/context.go:605:29: undefined: binding.Header | ||
+ | vendor/github.com/gin-gonic/gin/context.go:659:31: undefined: binding.YAML | ||
+ | vendor/github.com/gin-gonic/gin/context.go:664:31: undefined: binding.Header | ||
+ | vendor/github.com/gin-gonic/gin/context.go:673:9: undefined: binding.Uri | ||
+ | vendor/github.com/gin-gonic/gin/context.go:896:17: undefined: render.PureJSON | ||
+ | vendor/github.com/gin-gonic/gin/context.go:912:17: undefined: render.ProtoBuf | ||
+ | vendor/github.com/gin-gonic/gin/context.go:1027:7: undefined: binding.MIMEYAML | ||
+ | vendor/github.com/gin-gonic/gin/mode.go:83:2: undefined: binding.EnableDecoderDisallowUnknownFields | ||
+ | |||
+ | </pre> | ||
+ | |||
+ | =see also= | ||
+ | |||
+ | |||
+ | [https://www.jianshu.com/p/a3f63b5da74c Gin实战:Gin+Mysql简单的Restful风格的API] | ||
+ | |||
+ | |||
+ | [https://blog.csdn.net/qq_34284638/article/details/104944319 Golang 入门-Gin框架安装及使用] | ||
+ | |||
+ | |||
+ | |||
+ | [https://blog.csdn.net/VanciorH/article/details/96314896 Golang(不存在)的包管理] | ||
+ | |||
+ | |||
+ | ==go mod== | ||
+ | [https://www.cnblogs.com/apocelipes/p/9534885.html golang包管理解决之道——go modules初探 ] | ||
+ | |||
+ | [https://blog.csdn.net/yangyangye/article/details/97243826 go mod常用命令 以及 常见问题] | ||
+ | |||
+ | [https://blog.csdn.net/sanbingyutuoniao123/article/details/100903028 go语言web框架gin安装(go mod方式)] | ||
+ | |||
+ | ==放弃govendor== | ||
+ | [https://blog.csdn.net/huwh_/article/details/77169858 Golang包管理工具(一)之govendor的使用] | ||
+ | |||
+ | [https://blog.csdn.net/studyhard232/article/details/81637607 govendor的安装与实践] | ||
+ | |||
+ | [https://my.oschina.net/u/3628490/blog/2245119 golang使用govendor教程] | ||
+ | |||
+ | [https://www.cnblogs.com/shockerli/p/go-package-manage-tool-govendor.html Go 包依赖管理工具 —— govendor] | ||
+ | |||
+ | [https://zhuanlan.zhihu.com/p/59191567 Go Mod对比Go Vendor] | ||
+ | |||
+ | [https://www.jianshu.com/p/88669ba57d04 Govendor使用] |
2021年12月9日 (四) 05:51的最新版本
install
https://github.com/goproxy/goproxy.cn
pre install golang
#apt #可以指定版本的 sudo apt install golang sudo apt install golang-go gccgo-go sudo apt install golang-go
FQ
tail /etc/profile #export http_proxy=http://192.168.10.173:8888 #export https_proxy=http://192.168.10.173:8888 source /etc/profile
install Gin package
export PATH=/usr/local/go/bin/:$PATH export GOPATH=/root/go #if U user is root
If you're in China
如果您使用的 Go 版本是 1.13 及以上
#nov 29 2021 export GO111MODULE=on export GOPROXY=https://goproxy.cn or echo "export GO111MODULE=on" >> ~/.profile echo "export GOPROXY=https://goproxy.cn" >> ~/.profile source ~/.profile go env -w GO111MODULE=on #注意这个打开后面可能会问题导致 go env -w GOPROXY=https://goproxy.io,direct #or go env -w GOPROXY=https://mirrors.aliyun.com/goproxy/,direct # 设置不走 proxy 的私有仓库,多个用逗号相隔(可选) go env -w GOPRIVATE=*.corp.example.com # 设置不走 proxy 的私有组织(可选) go env -w GOPRIVATE=example.com/org_name
Go 版本是 1.12 及以下
# 启用 Go Modules 功能 export GO111MODULE=on # 配置 GOPROXY 环境变量 export GOPROXY=https://goproxy.io
或者,根据文档可以把上面的命令写到.profile或.bash_profile文件中长期生效
初始化 gin
go mod
go env -w GO111MODULE=on #注意这个打开后面可能会问题导致 go env -w GOPROXY=https://goproxy.io,direct cd mygo/ mkdir ginweb cd ginweb/ go mod init ginweb(名字自定义) go get -u github.com/gin-gonic/gin #注意 ,用go mod 来管理 june 08 2020 goverdor 已过期 请用 go mod 安装一般用下面的goverdor 版本管理 tool 去安装 这一块可以直接跳过 go run example.go ps evan@myxps:~/mygo/ginweb$ echo $GOROOT /home/evan/go evan@myxps:~/mygo/ginweb$ echo $GOPATH /home/evan/go #别人的例子 编写 HelloWorld 文件,测试运行环境。 package main import "fmt" func main(){ fmt.Println("hello,world!") } go run hello.go go build hello.go 创建 go.mod 文件 go mod init hello 用 Gin 实现一个简单的 http 服务 import ( "gopkg.in/gin-gonic/gin.v1" "net/http" ) func main(){ router := gin.Default() router.GET("/", func(c *gin.Context) { c.String(http.StatusOK, "Hello World") }) router.Run(":8000") } 直接编译执行 go run hello 可以看到引用的包都被自动下载了
放弃了 Use a vendor tool like Govendor
go get -u -v github.com/kardianos/govendor go get github.com/kardianos/govendor #在墙外非常快
sudo apt install govendor # apt
mkdir -p $GOPATH/src/github.com/myusername/project && cd "$_"
Vendor init your project and add gin
govendor init govendor fetch github.com/gin-gonic/[email protected] #有时要绝对路径 @后面也可以不加 有时一定要加 /root/go/bin/govendor fetch github.com/gin-gonic/[email protected]
learn
go官方推出的gin教程 https://docs.studygolang.com/doc/tutorial/web-service-gin
https://go.dev/doc/tutorial/web-service-gin
https://github.com/astaxie/build-web-application-with-golang
https://github.com/avelino/awesome-go
https://gin-gonic.com/docs/quickstart/
https://github.com/gin-gonic/examples
https://learnku.com/docs/gin-gonic/2019
https://godoc.org/golang.org/x/tools/cmd
IDE
golnad and 50.Go Mod 来创建 Gin 项目
trouble
working directory is not part of a module
关闭go代理就行了 或者 google ct$ go run main.go main.go:6:2: cannot find module providing package github.com/gin-gonic/gin: working directory is not part of a module evan@myxps:~/go/src/github.com/myusername/project$ go env -w GO111MODULE=off go run 2.go 2.go:3:8: cannot find module providing package github.com/gin-gonic/gin: working directory is not part of a module evan@myxps:~/data/devops/go/tmp$ echo $GOROOT evan@myxps:~/data/devops/go/tmp$ go mod init gin go: creating new go.mod: module gin evan@myxps:~/data/devops/go/tmp$ ls 1.go 2.go go.mod evan@myxps:~/data/devops/go/tmp$ go mod vendor go: finding module for package github.com/gin-gonic/gin go: found github.com/gin-gonic/gin in github.com/gin-gonic/gin v1.7.7
1.6 err
# go run main.go # github.com/myusername/project/vendor/github.com/gin-gonic/gin vendor/github.com/gin-gonic/gin/context.go:36:26: undefined: binding.MIMEYAML vendor/github.com/gin-gonic/gin/context.go:600:29: undefined: binding.YAML vendor/github.com/gin-gonic/gin/context.go:605:29: undefined: binding.Header vendor/github.com/gin-gonic/gin/context.go:659:31: undefined: binding.YAML vendor/github.com/gin-gonic/gin/context.go:664:31: undefined: binding.Header vendor/github.com/gin-gonic/gin/context.go:673:9: undefined: binding.Uri vendor/github.com/gin-gonic/gin/context.go:896:17: undefined: render.PureJSON vendor/github.com/gin-gonic/gin/context.go:912:17: undefined: render.ProtoBuf vendor/github.com/gin-gonic/gin/context.go:1027:7: undefined: binding.MIMEYAML vendor/github.com/gin-gonic/gin/mode.go:83:2: undefined: binding.EnableDecoderDisallowUnknownFields
see also
Gin实战:Gin+Mysql简单的Restful风格的API