Golang本地模块的使用与配置

Golang本地模块的使用与配置 按照以下文档中的步骤操作,在 Go 1.21 版本中未能成功。

go.dev

Call your code from another module - The Go Programming Language

从另一个模块调用你的代码 - Go 编程语言

感谢任何帮助。


更多关于Golang本地模块的使用与配置的实战教程也可以访问 https://www.itying.com/category-94-b0.html

7 回复

没问题。很高兴发现了 go mod 的这个功能。

更多关于Golang本地模块的使用与配置的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


感谢您的帮助。

谢谢。但根据教程,我们应该更改调用模块 hello。

在 hello 目录的命令提示符下,运行以下命令:

$ go mod edit -replace example.com/greetings=../greetings

供参考,执行命令后,我的 hello/go.mod 文件内容如下。

> go mod edit -replace example.com/greetings=../greetings

module example.com/hello

go 1.21.1

replace example.com/greetings => ../greetings

require example.com/greetings v0.0.0-00010101000000-000000000000

是的,我们正在修改 work/hello/go.mod。正如您在我第一行 go.mod 的代码片段中所看到的,这就是我们正在修改的模块。

replace 语句基本上负责将 hello.go 中的 import "example.com/greetings" 动态地更改为 import "../greetings"。像那样手动更改 hello.go 中的导入语句是另一种解决方案,但我认为 go mod 支持这种替换机制非常好。

func main() {
    fmt.Println("hello world")
}

我在 1.21.1 版本上试过了,对我来说是可行的。 你的链接指向了教程的中间部分。你检查过前面创建 greetings 模块的那个章节吗?

总之,你最终会得到这样的结构:

.
├── greetings
│  ├── go.mod           // 此文件的默认值
│  │                    // (由 go mod init 创建)是好的
│  └── greetings.go     // 包 greetings,即一个库/模块
└── hello
   ├── go.mod          // 使用 replace 指令,告诉此包中的 go 代码
   │                   // 在哪里可以找到本地的 "example.com/greetings"
   │                   // 而不是在线查找
   └── hello.go        // 包 main,即一个包含 main 函数的可执行文件

以下是我执行的步骤:

  1. 创建一个新的空工作目录 work
  2. greetings 模块 创建目录 work/greetings (package greetings)
  3. 在此目录中运行 go mod init example.com/greetings。这会为此模块创建一个 go.mod 文件。
  4. 将代码粘贴到 work/greetings/greetings.go
  5. hello 模块 创建目录 work/hello/ (package main)
  6. 在此目录中运行 go mod init example.com/hello。同样会创建一个 go.mod 文件。
  7. 编辑 work/greetings/go.mod,使其将代码中找到的所有对 example.com/greetings 的导入,实际指向 ../greetings (这对我来说是新的,这非常酷,以前我只是编辑我的 .go 代码,但显然你可以用 go.mod 文件进行重定向非常酷)
  8. work/hello 中运行 go run hello.go。(相当神奇的)Go 构建系统会完成剩下的所有工作。

对于第 7 步,对我来说新的是我们可以使用 go mod edit 来(更安全地)编辑 go.mod 文件。我过去曾手动编辑过那个文件,但阅读 go help mod edit 非常有趣。

根据你提供的文档链接,这是关于Go模块间调用的教程。在Go 1.21版本中,本地模块的使用配置基本一致,但需要注意一些细节。以下是具体步骤和示例代码:

首先,确保你的Go版本是1.21或更高:

go version

1. 创建主模块 创建一个新目录并初始化模块:

mkdir app
cd app
go mod init example.com/app

创建主程序文件 main.go

package main

import (
    "fmt"
    "example.com/greetings"
)

func main() {
    message := greetings.Hello("World")
    fmt.Println(message)
}

2. 创建依赖模块 在另一个目录中创建依赖模块:

cd ..
mkdir greetings
cd greetings
go mod init example.com/greetings

创建 greetings.go 文件:

package greetings

import "fmt"

func Hello(name string) string {
    return fmt.Sprintf("Hello, %s!", name)
}

3. 配置本地模块替换app/go.mod 中添加 replace 指令指向本地模块:

module example.com/app

go 1.21

replace example.com/greetings => ../greetings

require example.com/greetings v0.0.0-00010101000000-000000000000

然后运行 go mod tidy 同步依赖:

cd ../app
go mod tidy

4. 验证配置 检查 app/go.mod 文件应该包含:

module example.com/app

go 1.21

replace example.com/greetings => ../greetings

require example.com/greetings v0.0.0-00010101000000-000000000000

5. 运行程序

go run main.go

预期输出:Hello, World!

如果遇到问题,检查以下几点:

  • 确保两个模块都在 GOPATH 之外
  • 确认 go.mod 文件中的模块路径正确
  • 检查文件路径是否正确(特别是 replace 指令中的相对路径)

常见问题解决方案:

# 清理模块缓存
go clean -modcache

# 重新初始化模块
rm go.mod go.sum
go mod init example.com/app

如果仍然失败,请提供具体的错误信息以便进一步诊断。

回到顶部