在使用golang进行项目开发过程中,经常会使用go module来进行依赖包的管理。本章讲述一下go module的使用。

1. 解决golang.org不能访问

在国内golang.org被屏蔽了,直接访问不了,我们采用如下办法解决:在http://ping.eu/ping/上ping一下golang.org,获取到IP如下图所示:

go-ping-eu

然后,在C:\Windows\System32\drivers\etc\hosts中,添加ip和golang.org的对应关系,添加如下一行:

# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

# localhost name resolution is handled within DNS itself.
#	127.0.0.1       localhost
#	::1             localhost
#140.82.113.4    github.com
#140.82.113.4    www.github.com
#199.232.69.194  github.global.ssl.fastly.net
#199.232.69.194  github.com.global.ssl.fastly.net
#199.232.69.194  www.github.global.ssl.fastly.net
#185.199.108.153 assets-cdn.github.com
#185.199.109.153 assets-cdn.github.com
#185.199.110.153 assets-cdn.github.com
#185.199.111.153 assets-cdn.github.com

#142.250.74.113 blog.golang.org
142.250.74.113 golang.org
#216.239.38.21 go.dev
#142.250.74.19 pkg.go.dev
#142.250.74.113 play.golang.org

然后启动windows命令行,执行:

# ipconfig/flushdns

之后重新启动下浏览器,就可访问golang.org了。

2. 如何在go.mod中指定使用特定版本的library

如下是来自Deepseek关于“go.mod 如何查看library 有哪些版本,然后指定使用特定的版本”的回答:

在 Go 的模块系统(Go Modules)中,可以通过 go.mod 文件明确指定依赖库的版本。以下是详细的操作方法和规则。

2.1 基本语法

在 go.mod 文件中,每个依赖项的格式为:

module [your-module-name]

go [go-version]

require (
    [module-path] [version]
)

示例如下:

module example.com/myapp

go 1.21

require (
    github.com/gin-gonic/gin v1.9.1
    github.com/stretchr/testify v1.8.4 // indirect
)

2.2 指定依赖版本的几种方式

  1. 直接指定语义化版本(SemVer)

    • 格式: vX.Y.Z(如 v1.2.3)

    • 示例

      require github.com/gin-gonic/gin v1.9.1
      
  2. 指定分支或提交哈希

    • 格式:[module-path]@[branch-name][module-path]@[commit-hash]

    • 示例

      require github.com/gin-gonic/gin@master          // 使用分支
      require github.com/gin-gonic/gin@daa7c04131f5    // 使用提交哈希
      
  3. 伪版本(Pseudo-Version)

    当依赖库没有正式的版本标签时,Go 会自动生成伪版本,格式为:vX.Y.Z-yyyymmddhhmmss-abcdefabcdef

    示例:

     require golang.org/x/text v0.0.0-20191109021931-daa7c04131f5
    

    请注意上面格式:

    • yyyymmddhhmmss: 年月日时分秒的字符串表示,长度12字节
    • abcdefabcdef: 为commit hash,取前12字节
  4. 使用 latest 或 upgrade 关键字

    • latest:拉取最新版本(不推荐,可能导致不稳定)

    • upgrade:允许升级到更高版本(需配合 go get 命令)

    • 示例

      go get -u github.com/gin-gonic/gin@latest
      

2.3 如何查看一个library有哪些版本

  1. 使用go list -m -versions命令查看

    使用go list -m -versions命令列出某个库的所有已发布版本:

    go list -m -versions github.com/gin-gonic/gin
    

    示例输出如下:

    github.com/gin-gonic/gin v1.1.1 v1.1.2 v1.1.3 v1.1.4 v1.3.0 v1.4.0 ... v1.9.1
    

    说明:

    • 此命令会列出该库的所有语义化版本(SemVer)标签
    • 如果库没有版本标签(如仅通过 master 分支开发),可能需要查看分支或提交哈希
  2. 查看分支或提交哈希(无版本标签时)

    如果库没有正式版本标签,可以通过以下方式获取最新代码:

    1.1 查看分支列表(需克隆仓库)

      # git clone https://github.com/gin-gonic/gin
      # cd gin
      # git branch -a
    

    1.2 查看tags列表

      # git tag
      # git checkout tags/aaa
    

    注: 不在go.mod中使用此方式来指定library

    1.3 查看提交历史

     # git log --oneline
    



[参看]

  1. go module官网

  2. go module官网(国内可访问)

  3. Using Go Modules

  4. go module基本使用

  5. golang github官网

  6. go mod使用

  7. 解决golang.org不能访问的问题