无法从同一目录中的文件中导入函数。

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

Unable to import a function from file in same directory

问题

我正在翻译你提供的内容,请稍等片刻。

英文:

I am following a tutorial and stuck on this. I also tried to go through official docs, but was unable to find out that's wrong.

Before posting here i found out that GOPATH also needs to be configured.

GOPATH: A:\GO

Path to utils folder: A:\GO\fem-intro-to-go\05_toolkit\code\utils

There are 2 files in utils, math.go and add_test.go.

math.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.go

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 code is giving an error in add_test.go: undeclared name: Add

Full Description of error:

{
	"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
}

答案1

得分: 1

使用go 1.16.6构建代码时,我没有看到任何问题。如果我开始一个新的go项目:

mkdir example
cd example
go mod init example

然后将你的代码放在utils/目录中,并将以下内容放在main.go中:

package main

import (
	"example/utils"
	"fmt"
)

func main() {
	answer := utils.Add(1, 2, 3)
	fmt.Printf("得到的答案:%d\n", answer)
}

这样我就有了以下布局:

$ tree .
.
├── example
├── go.mod
├── main.go
└── utils
    ├── add_test.go
    └── math.go

你的代码编译时出现了问题:

$ go build

然后我运行生成的二进制文件:

$ ./example
当前数字:1
当前数字:2
当前数字:3
得到的答案:6

我想指出的是,我没有设置GOPATH或任何其他GO*环境变量来使其工作。

英文:

I don't see any problems with your code when building it using go 1.16.6. If I start a new go project:

mkdir example
cd example
go mod init example

Then place your code in the utils/ directory, and place the following in main.go:

package main

import (
	"example/utils"
	"fmt"
)

func main() {
	answer := utils.Add(1, 2, 3)
	fmt.Printf("got answer: %d\n", answer)
}

So that I have the follow layout:

$ tree .
.
├── example
├── go.mod
├── main.go
└── utils
    ├── add_test.go
    └── math.go

Your code compiles with a problem:

$ go build

And I run the resulting binary:

$ ./example
Current Number: 1
Current Number: 2
Current Number: 3
got answer: 6

I would like to point out that I haven't set GOPATH or any other GO* environment variables for this to work.

huangapple
  • 本文由 发表于 2021年8月16日 01:46:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/68794015.html
匿名

发表评论

匿名网友

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

确定