我正在遵循教程并坚持这一点。我也试图通过官方文档,但无法发现这是错误的。
在这里发布之前,我发现GOPATH也需要配置。
走道: A:\GO
实用程序文件夹的路径:A:\GO\fem-intro-to-go\05_toolkit\代码\utils
有 2 个文件在 utils, 数学.go 和 add_test.go.
数学.go
package utils
import "fmt"
func printNum(num int) {
fmt.Println("Current Number:", num)
}
// Add adds together multiple numbers
func Add(nums ...int) int {
total := 0
for _, v := range nums {
printNum(v)
total += v
}
return total
}
add_test
package utils
import "testing"
func TestAdd(t *testing.T) {
expected := 4
actual := Add(2, 2)
if actual != expected {
t.Errorf("Add function does not add up: Expected: %d, Actual: %d", expected, actual)
}
}
VS 代码在 add_test.go 中给出错误:未声明的名称:添加
错误的完整描述:
{
"resource": "/a:/GO/fem-intro-to-go/05_toolkit/code/utils/add_test.go",
"owner": "_generated_diagnostic_collection_name_#1",
"code": {
"value": "UndeclaredName",
"target": {
"$mid": 1,
"external": "https://pkg.go.dev/golang.org/x/tools/internal/typesinternal?utm_source%3Dgopls#UndeclaredName",
"path": "/golang.org/x/tools/internal/typesinternal",
"scheme": "https",
"authority": "pkg.go.dev",
"query": "utm_source=gopls",
"fragment": "UndeclaredName"
}
},
"severity": 8,
"message": "undeclared name: Add",
"source": "compiler",
"startLineNumber": 9,
"startColumn": 12,
"endLineNumber": 9,
"endColumn": 15
}
慕侠2389804
相关分类