英文:
Undefined Variables Within a Package During Build
问题
我有一个名为db
的包中有两个文件,其中一个定义了一些未导出的变量。另一个是一个测试文件,需要使用这些变量,像这样:
(这是项目的结构)
$GOPATH/src/gitlab.com/myname/projectdir
├── main.go
└── db
├── add.go
└── add_test.go
(这是文件的简略版本)
db/add.go
package db
func Add(x, y int) int {
return x + y
}
// 其他一些使用`add_test.go`中的a和b的函数
db/add_test.go
package db
import (
"testing"
)
var (
a = 1
b = 2
)
// 测试函数使用`add.go`中的变量
func testAdd(t *testing.T) {
result := add(a, b)
if result != 3 {
t.Error(err)
}
}
在db/
目录下运行go test
通过了,但是当我运行go run main.go
时,产生了以下错误:
db/add.go:
: undefined: a
db/add.go:: undefined: b
似乎add.go
在构建过程中找不到add_test.go
中的a
和b
。
main.go
package main
import (
"fmt"
"gitlab.com/myname/projectdir/db"
)
func main() {
res := db.Add(1, 2)
fmt.Println(res)
}
这是因为构建过程中没有包含add_test.go
吗?
英文:
I have two files within one package named db
, one of which has a few unexported variables defined. Another one is a test file and would need to use these variables like so:
(This is the structure of the project)
$GOPATH/src/gitlab.com/myname/projectdir
├── main.go
└── db
├── add.go
└── add_test.go
(Here is a terse variation of the files)
db/add.go
package db
func Add(x, y int) int {
return x + y
}
// some other functions that use a and b from `add_test.go`
db/add_test.go
package db
import (
"testing"
)
var (
a = 1
b = 2
)
// test function use variables from add.go
func testAdd(t *testing.T) {
result := add(a, b)
if result != 3 {
t.Error(err)
}
}
Running go test
within db/
directory passed, but once I ran go run main go
it produced the following error:
> db/add.go:<linenum>: undefined: a
> db/add.go:<linenum>: undefined: b
Seems like add.go
cannot find a
and b
from add_test.go
during the build.
main.go
package main
import (
"fmt"
"gitlab.com/myname/projectdir/db"
)
func main() {
res := db.Add(1, 2)
fmt.Println(res)
}
Is this because add_test.go
is not included during the build?
答案1
得分: 2
这只是go工具的工作方式。
只有在运行go test
时,才会编译_test.go
文件。当一个包被另一个包导入时,它的_test.go
文件中的任何代码都不会被使用。
尝试在db包内部运行go build
或go install
命令,它将失败。
英文:
This is just the way how go tool works.
_test.go
files are compiled only when you run go test
. When a package is imported from another package any code from its _test.go
files is not used.
Try running go build
or go install
from inside db package. It will fail.
答案2
得分: 1
相对路径在Go语言中有些棘手。首先,我认为你需要在路径前面加上import "./db"
。另外一件事是,你应该在$GOPATH/src
目录下。
请尝试以下操作:
- 将你的文件移动到
$GOPATH/src/project
和$GOPATH/src/project/db
目录下。 - 在导入路径中使用
./db
前缀来导入DB包。
至于IDE,这完全取决于你使用的插件。尝试自己运行工具:golint
、go vet
、oracle
等,以查看实际的Go警告和错误。
英文:
Relative paths are touchy in Go. For one, I think you need to prefix them with import "./db"
. Another thing is that you should be in your $GOPATH/src location.
Try this:
- move your files under the
$GOPATH/src/project
and$GOPATH/src/project/db
directories. - prefix your import path with
./db
for the DB package.
As for the IDE, that's all up to whatever plugins you are using. Try running the tools yourself: golint
, go vet
, oracle
, etc to see the actual go warnings and errors.
答案3
得分: 0
测试函数应该以Test开头,这是文档中所说的。
func TestAdd(t *testing.T) {
result := Add(a, b)
if result != 3 {
t.Errorf("expected 3, got %d", result)
}
}
祝好。
英文:
Test functions should start with Test. that is what the documentation says.
func TestAdd(t *testing.T) {
result := Add(a, b)
if result != 3 {
t.Errorf("expected 3, got %d ", result)
}
}
Cheers.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论