Golang模块与本地包的导入问题解析

Golang模块与本地包的导入问题解析 大家好

我在使用 Go 模块时遇到了问题。

我的项目结构示例如下:

testmod/
------bar/
--------- bar.go
--------- bar_test.go
------cmd/
--------- testmod/
--------------- main.go
------foo/
--------- foo.go
--------- foo_test.go
------go.mod
------vendor

我的 go.mod 定义如下:

module example.org/maadam/testmod

require (
	github.com/davecgh/go-spew v1.1.1 // indirect
	github.com/pmezard/go-difflib v1.0.0 // indirect
	github.com/stretchr/testify v1.2.2
)

foo.go

package foo

import (
    "strconv"
)

func ConvertInt2String(num int) string {
	return strconv.Itoa(num)
}

bar.go

package bar

import (
    "strconv"
)

func ConvertString2Int(num string) (int, error) {
	return strconv.Atoi(num)
}

main.go

package main

import (
	"fmt"

	"example.org/maadam/testmod/foo"
	"example.org/maadam/testmod/bar"
)

func main() {
	iFoo := 2
	strFoo := ConvertInt2String(iFoo)
	fmt.Printf("foo.ConvertInt2String %d : %s", iFoo, strFoo)

	strBar := "28"
	iBar, _ := ConvertString2Int(strBar)
	fmt.Printf("bar.ConvertString2Int %s : %d", strBar, iBar)
}

我以为我的模块名是 example.org/maadam/testmod,我就可以通过使用 example.org/maadam/testmod/foo 或 /bar 来导入我的 foo 或 bar 包。 但是当我尝试构建 cmd/testmod/main.go 时,出现了以下错误:

> go build cmd/testmod/main.go 
> # command-line-arguments
> cmd/testmod/main.go:6:2: imported and not used: "example.org/maadam/testmod/foo"
> cmd/testmod/main.go:7:2: imported and not used: "example.org/maadam/testmod/bar"
> cmd/testmod/main.go:12:12: undefined: ConvertInt2String
> cmd/testmod/main.go:16:13: undefined: ConvertString2Int

你能帮我理解在使用 Go 模块导入包时我做错了什么吗?或者只是项目结构不兼容?

此致


更多关于Golang模块与本地包的导入问题解析的实战教程也可以访问 https://www.itying.com/category-94-b0.html

2 回复

好的,我发现了自己的错误……我必须使用导入的包名来调用函数:foo.ConvertInt2String() 和 bar.ConvertString2Int()……

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

更多关于Golang模块与本地包的导入问题解析的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


在Go模块中导入本地包时,需要正确使用包名和函数调用方式。你的问题在于main.go中函数调用的方式不正确,以及可能缺少必要的go.mod配置。

以下是修正后的代码:

1. 修正main.go中的导入和函数调用:

package main

import (
	"fmt"

	"example.org/maadam/testmod/foo"
	"example.org/maadam/testmod/bar"
)

func main() {
	iFoo := 2
	strFoo := foo.ConvertInt2String(iFoo) // 使用foo包名前缀
	fmt.Printf("foo.ConvertInt2String %d : %s\n", iFoo, strFoo)

	strBar := "28"
	iBar, _ := bar.ConvertString2Int(strBar) // 使用bar包名前缀
	fmt.Printf("bar.ConvertString2Int %s : %d\n", strBar, iBar)
}

2. 确保go.mod文件正确配置:

module example.org/maadam/testmod

go 1.19

require (
	github.com/stretchr/testify v1.2.2
)

require (
	github.com/davecgh/go-spew v1.1.1 // indirect
	github.com/pmezard/go-difflib v1.0.0 // indirect
)

3. 构建命令应该从项目根目录执行:

cd /path/to/testmod
go build ./cmd/testmod

或者构建并运行:

go run ./cmd/testmod

4. 完整的项目结构验证:

确保每个目录都有正确的包声明:

  • foo/foo.go 第一行:package foo
  • bar/bar.go 第一行:package bar
  • cmd/testmod/main.go 第一行:package main

错误分析:

  • 原始错误是因为直接调用ConvertInt2StringConvertString2Int而没有使用包名前缀
  • Go要求使用包名.函数名的格式调用其他包中的函数
  • 导入路径example.org/maadam/testmod/fooexample.org/maadam/testmod/bar是正确的,但函数调用需要加上包名前缀

修正后的代码应该能正常编译和运行。

回到顶部