Go语言自学系列 | golang包管理工具go module
Posted COCOgsta
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Go语言自学系列 | golang包管理工具go module相关的知识,希望对你有一定的参考价值。
视频来源:B站《golang入门到项目实战 [2021最新Go语言教程,没有废话,纯干货!持续更新中...]》
一边学习一边整理老师的课程内容及试验笔记,并与大家分享,请移步至知乎网站,谢谢支持!
附上汇总贴:Go语言自学系列 | 汇总 - 知乎 (zhihu.com)https://zhuanlan.zhihu.com/p/484035621
Go module简介
Go modules是golang 1.11新加的特性,用来管理模块中包的依赖关系。
Go mod使用方法
- 初始化模块
Go mod init <项目模块名称>
- 依赖关系处理,根据go.mod文件
Go mod tidy
- 将依赖包复制到项目的vendor目录
Go mod vendor
如果报被屏蔽(墙),可以使用这个命令,随后使用go build -mod=vendor编译
- 显示依赖关系
Go list -m all
- 显示详细依赖关系
Go list -m -json all
- 下载依赖
Go mod download [path@version]
[path@version]是非必写的
实例演示
打开终端,在pro02文件夹下输入go mode init http://duo360.com/pro02
PS D:\\SynologyDrive\\软件开发\\go\\golang入门到项目实战\\goproject\\360duote.com\\pro02> go mod init duo360.com/pro02
go: creating new go.mod: module duo360.com/pro02
go: to add module requirements and sums:
go mod tidy
编辑service/user_service.go
package service
import "fmt"
// code
func TestUserService()
fmt.Println("test user service...")
service目录执行go build
PS D:\\SynologyDrive\\软件开发\\go\\golang入门到项目实战\\goproject\\360duote.com\\pro02> cd .\\service\\
PS D:\\SynologyDrive\\软件开发\\go\\golang入门到项目实战\\goproject\\360duote.com\\pro02\\service> go build
main.go中引入service这个包
package main
import (
"fmt"
"duo360.com/pro02/service"
)
func main()
fmt.Println("hello world")
service.TestUserService()
运行结果
[Running] go run "d:\\SynologyDrive\\软件开发\\go\\golang入门到项目实战\\goproject\\360duote.com\\pro02\\main.go"
hello world
test user service...
编辑customer_service.go
package service
import "fmt"
func TestCustomerService()
fmt.Println("test customer service...")
编辑完后再编译一次,产生缓存
PS D:\\SynologyDrive\\软件开发\\go\\golang入门到项目实战\\goproject\\360duote.com\\pro02\\service> go build
此时main.go中就可以调用其新增的函数了
package main
import (
"fmt"
"duo360.com/pro02/service"
)
func main()
fmt.Println("hello world")
service.TestUserService()
service.TestCustomerService()
运行结果
[Running] go run "d:\\SynologyDrive\\软件开发\\go\\golang入门到项目实战\\goproject\\360duote.com\\pro02\\tempCodeRunnerFile.go"
hello world
test user service...
test customer service...
导入远程包,执行 go get -u http://github.com/gin-gonic/gin 下载
PS D:\\SynologyDrive\\软件开发\\go\\golang入门到项目实战\\goproject\\360duote.com\\pro02\\service> go get -u github.com/gin-gonic/gin
go: downloading github.com/ugorji/go v1.2.7
go: downloading github.com/ugorji/go/codec v1.2.7
go: downloading golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9
go: downloading golang.org/x/crypto v0.0.0-20220214200702-86341886e292
go get: added github.com/gin-gonic/gin v1.7.7
go get: added github.com/go-playground/validator/v10 v10.10.0
go get: added github.com/golang/protobuf v1.5.2
go get: added github.com/json-iterator/go v1.1.12
go get: added github.com/mattn/go-isatty v0.0.14
go get: added github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd
go get: added github.com/ugorji/go v1.2.7
go get: added golang.org/x/crypto v0.0.0-20220214200702-86341886e292
go get: added golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9
go get: added golang.org/x/text v0.3.7
go get: added google.golang.org/protobuf v1.27.1
go get: added gopkg.in/yaml.v2 v2.4.0
修改main.go
package main
import (
"github.com/gin-gonic/gin"
)
func main()
/* fmt.Println("hello world")
service.TestUserService()
service.TestCustomerService() */
r := gin.Default()
r.GET("/ping", func(c *gin.Context)
c.JSON(200, gin.H
"message": "pong",
)
)
r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
移动pro02目录,执行go mod tidy
PS D:\\SynologyDrive\\软件开发\\go\\golang入门到项目实战\\goproject\\360duote.com\\pro02> go mod tidy
go: downloading github.com/stretchr/testify v1.7.0
go: downloading github.com/google/go-cmp v0.5.5
go: downloading gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c
go: downloading github.com/kr/pretty v0.3.0
go: downloading gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
go: downloading github.com/rogpeppe/go-internal v1.8.0
go: downloading github.com/kr/text v0.2.0
运行后访问localhost:8080/ping
[Running] go run "d:\\SynologyDrive\\软件开发\\go\\golang入门到项目实战\\goproject\\360duote.com\\pro02\\tempCodeRunnerFile.go"
[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.
[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
- using env: export GIN_MODE=release
- using code: gin.SetMode(gin.ReleaseMode)
[GIN-debug] GET /ping --> main.main.func1 (3 handlers)
[GIN-debug] [WARNING] You trusted all proxies, this is NOT safe. We recommend you to set a value.
Please check https://pkg.go.dev/github.com/gin-gonic/gin#readme-don-t-trust-all-proxies for details.
[GIN-debug] Environment variable PORT is undefined. Using port :8080 by default
[GIN-debug] Listening and serving HTTP on :8080
[GIN] 2022/03/05 - 20:36:41 | 404 | 0s | ::1 | GET "/"
[GIN] 2022/03/05 - 20:36:41 | 404 | 0s | ::1 | GET "/favicon.ico"
[GIN] 2022/03/05 - 20:36:52 | 200 | 465µs | ::1 | GET "/ping"
以上是关于Go语言自学系列 | golang包管理工具go module的主要内容,如果未能解决你的问题,请参考以下文章