(18条消息) Go语言自学系列 | golang包管理工具go module_COCOgsta的博客-CSDN博客_golang 包管理器



Go语言自学系列 | golang包管理工具go module

COCOgsta 已于 2022-04-09 21:02:19 修改 370 收藏
分类专栏: 视频学习笔记 文章标签: golang go

视频来源:B站《golang入门到项目实战 [2021最新Go语言教程,没有废话,纯干货!持续更新中...]》

一边学习一边整理老师的课程内容及试验笔记,并与大家分享,侵权即删,谢谢支持!

附上汇总贴:Go语言自学系列 | 汇总_COCOgsta的博客-CSDN博客


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

  1. 1
    PS D:\SynologyDrive\软件开发\go\golang入门到项目实战\goproject\360duote.com\pro02> go mod init duo360.com/pro02
  2. 2
    go: creating new go.mod: module duo360.com/pro02
  3. 3
    go: to add module requirements and sums:
  4. 4
    go mod tidy

编辑service/user_service.go

  1. 1
    package service
  2. 2
  3. 3
    import "fmt"
  4. 4
  5. 5
    // code
  6. 6
  7. 7
    func TestUserService() {
  8. 8
    fmt.Println("test user service...")
  9. 9
    }
  10. 10

service目录执行go build

  1. 1
    PS D:\SynologyDrive\软件开发\go\golang入门到项目实战\goproject\360duote.com\pro02> cd .\service\
  2. 2
    PS D:\SynologyDrive\软件开发\go\golang入门到项目实战\goproject\360duote.com\pro02\service> go build

main.go中引入service这个包

  1. 1
    package main
  2. 2
  3. 3
    import (
  4. 4
    "fmt"
  5. 5
  6. 6
    "duo360.com/pro02/service"
  7. 7
    )
  8. 8
  9. 9
    func main() {
  10. 10
    fmt.Println("hello world")
  11. 11
  12. 12
    service.TestUserService()
  13. 13
    }
  14. 14

运行结果

  1. 1
    [Running] go run "d:\SynologyDrive\软件开发\go\golang入门到项目实战\goproject\360duote.com\pro02\main.go"
  2. 2
    hello world
  3. 3
    test user service...

编辑customer_service.go

  1. 1
    package service
  2. 2
  3. 3
    import "fmt"
  4. 4
  5. 5
    func TestCustomerService() {
  6. 6
    fmt.Println("test customer service...")
  7. 7
    }
  8. 8

编辑完后再编译一次,产生缓存

PS D:\SynologyDrive\软件开发\go\golang入门到项目实战\goproject\360duote.com\pro02\service> go build  
 

此时main.go中就可以调用其新增的函数了

  1. 1
    package main
  2. 2
  3. 3
    import (
  4. 4
    "fmt"
  5. 5
  6. 6
    "duo360.com/pro02/service"
  7. 7
    )
  8. 8
  9. 9
    func main() {
  10. 10
    fmt.Println("hello world")
  11. 11
  12. 12
    service.TestUserService()
  13. 13
    service.TestCustomerService()
  14. 14
    }
  15. 15
  16. 16

运行结果

  1. 1
    [Running] go run "d:\SynologyDrive\软件开发\go\golang入门到项目实战\goproject\360duote.com\pro02\tempCodeRunnerFile.go"
  2. 2
    hello world
  3. 3
    test user service...
  4. 4
    test customer service...

导入远程包,执行 go get -u http://github.com/gin-gonic/gin 下载

  1. 1
    PS D:\SynologyDrive\软件开发\go\golang入门到项目实战\goproject\360duote.com\pro02\service> go get -u github.com/gin-gonic/gin
  2. 2
    go: downloading github.com/ugorji/go v1.2.7
  3. 3
    go: downloading github.com/ugorji/go/codec v1.2.7
  4. 4
    go: downloading golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9
  5. 5
    go: downloading golang.org/x/crypto v0.0.0-20220214200702-86341886e292
  6. 6
    go get: added github.com/gin-gonic/gin v1.7.7
  7. 7
    go get: added github.com/go-playground/validator/v10 v10.10.0
  8. 8
    go get: added github.com/golang/protobuf v1.5.2
  9. 9
    go get: added github.com/json-iterator/go v1.1.12
  10. 10
    go get: added github.com/mattn/go-isatty v0.0.14
  11. 11
    go get: added github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd
  12. 12
    go get: added github.com/ugorji/go v1.2.7
  13. 13
    go get: added golang.org/x/crypto v0.0.0-20220214200702-86341886e292
  14. 14
    go get: added golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9
  15. 15
    go get: added golang.org/x/text v0.3.7
  16. 16
    go get: added google.golang.org/protobuf v1.27.1
  17. 17
    go get: added gopkg.in/yaml.v2 v2.4.0

修改main.go

  1. 1
    package main
  2. 2
  3. 3
    import (
  4. 4
    "github.com/gin-gonic/gin"
  5. 5
    )
  6. 6
  7. 7
    func main() {
  8. 8
    /* fmt.Println("hello world")
  9. 9
  10. 10
    service.TestUserService()
  11. 11
    service.TestCustomerService() */
  12. 12
    r := gin.Default()
  13. 13
    r.GET("/ping", func(c *gin.Context) {
  14. 14
    c.JSON(200, gin.H{
  15. 15
    "message": "pong",
  16. 16
    })
  17. 17
    })
  18. 18
    r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
  19. 19
    }
  20. 20

移动pro02目录,执行go mod tidy

  1. 1
    PS D:\SynologyDrive\软件开发\go\golang入门到项目实战\goproject\360duote.com\pro02> go mod tidy
  2. 2
    go: downloading github.com/stretchr/testify v1.7.0
  3. 3
    go: downloading github.com/google/go-cmp v0.5.5
  4. 4
    go: downloading gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c
  5. 5
    go: downloading github.com/kr/pretty v0.3.0
  6. 6
    go: downloading gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
  7. 7
    go: downloading github.com/rogpeppe/go-internal v1.8.0
  8. 8
    go: downloading github.com/kr/text v0.2.0

运行后访问localhost:8080/ping

  1. 1
    [Running] go run "d:\SynologyDrive\软件开发\go\golang入门到项目实战\goproject\360duote.com\pro02\tempCodeRunnerFile.go"
  2. 2
    [GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.
  3. 3
  4. 4
    [GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
  5. 5
    - using env: export GIN_MODE=release
  6. 6
    - using code: gin.SetMode(gin.ReleaseMode)
  7. 7
  8. 8
    [GIN-debug] GET /ping --> main.main.func1 (3 handlers)
  9. 9
    [GIN-debug] [WARNING] You trusted all proxies, this is NOT safe. We recommend you to set a value.
  10. 10
    Please check https://pkg.go.dev/github.com/gin-gonic/gin#readme-don-t-trust-all-proxies for details.
  11. 11
    [GIN-debug] Environment variable PORT is undefined. Using port :8080 by default
  12. 12
    [GIN-debug] Listening and serving HTTP on :8080
  13. 13
    [GIN] 2022/03/05 - 20:36:41 | 404 | 0s | ::1 | GET "/"
  14. 14
    [GIN] 2022/03/05 - 20:36:41 | 404 | 0s | ::1 | GET "/favicon.ico"
  15. 15
    [GIN] 2022/03/05 - 20:36:52 | 200 | 465µs | ::1 | GET "/ping"


yg9538 2022年7月22日 22:48 659 收藏文档