Golang:构建命令行参数:找不到路径x的模块。

3

我在main.go中运行来自非主要包的函数时遇到了问题。

// main.go
package main

import test "./tests"

func main() {
    test.Test("hello world")
}


// (relative to main.go) ./tests/test.go
package test

import "fmt"

func Test(str string) {
    fmt.Println(str)
}

输出结果:

构建命令行参数: 无法找到路径 _/c_/Users/Mike/Desktop/random/tests 的模块

2
在Go中不要使用相对导入。 - Adrian
1个回答

9

如果您使用的是Go 1.16+,请使用Go模块:

  1. 执行 go mod init 项目名称
  2. import test "./tests" 替换为 import test "项目名称/tests"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接