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

Sal*_*ani 3 go

我在 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)
}
Run Code Online (Sandbox Code Playgroud)

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

San*_*ago 9

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

  1. 执行者go mod init projectname
  2. 替换import test "./tests"import test "projectname/tests"