golang: build命令行参数: 找不到路径x的模块

huangapple go评论110阅读模式
英文:

golang: build command-line-arguments: cannot find module for path x

问题

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

  1. // main.go
  2. package main
  3. import test "./tests"
  4. func main() {
  5. test.Test("hello world")
  6. }
  7. // (相对于main.go) ./tests/test.go
  8. package test
  9. import "fmt"
  10. func Test(str string) {
  11. fmt.Println(str)
  12. }

输出:
build command-line-arguments: 无法找到路径 _/c_/Users/Mike/Desktop/random/tests 的模块

英文:

I am getting an issue running a function from a non-main package in main.go

  1. // main.go
  2. package main
  3. import test "./tests"
  4. func main() {
  5. test.Test("hello world")
  6. }
  7. // (relative to main.go) ./tests/test.go
  8. package test
  9. import "fmt"
  10. func Test(str string) {
  11. fmt.Println(str)
  12. }

Output:
build command-line-arguments: cannot find module for path _/c_/Users/Mike/Desktop/random/tests

答案1

得分: 9

如果您正在使用Go 1.16+,请使用Go模块:

  1. 执行 go mod init projectname
  2. import test " ./tests" 替换为 import test "projectname/tests"
英文:

If you are using Go 1.16+ use Go modules:

  1. Exec go mod init projectname
  2. Replace import test "./tests" by
    import test "projectname/tests"

huangapple
  • 本文由 发表于 2021年9月1日 03:31:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/69004403.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定