我已经安装了VSCode和Go插件。我试图把这个函数称为:
package main
import "fmt"
func printHello() {
fmt.Println("hello world")
}Go: Test file,这引发了一个No tests found. Current file is not a test file.错误.Go: Test function at cursor --这引发了同样的错误。_test.go命名约定的兄弟文件:package main
import (
"testing"
)
func TestHello(t *testing.T) {
msg, err := printHello()
}这引发了一个构建错误:c:\path\to\my_test.go:8:18: main() used as value。
然后,
return语句,它抛出了以下错误:go: cannot find main module, but found .git/config in c:\path\to\my_folder
to create a module there, run:
cd ..\.. && go mod initGo: Debug Test at Cursor函数和Go: Generate unit tests for function选项,这促使我安装了其他几个VSCode插件,并促使我升级Go本身。前两个命令调色板选项由于ENOENT错误而开始失败,因此我重新启动了VSCode,并获得了一个新的测试函数。我打开了VSCode调试控制台,但它不允许我在那里启动会话。GOPATH i右键单击父文件夹并选择Open in Integrated Terminal选项,该选项启动WSL会话- cd ..\.. && go mod init抛出与相关的错误。
me@COMPUTER:/mnt/c/path/to/my_folder$ go mod init
-bash: /c/go/bin/go.exe: No such file or directoryGo: Show current GOPATH确定WSL GOPATH指向的是什么--实际上,WSL和VSCode GOPATHs都是不正确的。GOPATH,然后我发射了错误的go mod init (以前VSCode的指导不正确):go: cannot determine module path for source directory C:\path\to\my_folder (outside GOPATH, module path must be specified)
Example usage:
'go mod init example.com/m' to initialize a v0 or v1 module
'go mod init example.com/m/v2' to initialize a v2 module
Run 'go help mod init' for more information.v2 Go模块初始化(我不知道为什么要选择v0或v1),它在项目的根目录中创建了一个go.mod,但是现在我看到了以下与包相关的错误:gopls requires a module at the root of your workspace.
You can work with multiple modules by opening each one as a workspace folder.
Improvements to this workflow will be coming soon, and you can learn more here:
https://github.com/golang/tools/blob/master/gopls/doc/workspace.md.如何在VSCode中调用Go函数?
发布于 2022-03-08 21:21:04
答案是:
*_test.go单元测试工具,并安装更多相邻的VSCode插件。go mod init blah。blah是模块的名称。对于GOPATH.go mod init main,这是有效的,但是VSCode稍后会抛出误导性错误(参见https://appliedgo.net/testmain/)。go.mod位于工作区的根目录,否则VSCode会遇到相对的文件错误。。
https://stackoverflow.com/questions/71400958
复制相似问题