在构建过程中包内的未定义变量

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

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中的ab

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 (
    &quot;fmt&quot;
    &quot;gitlab.com/myname/projectdir/db&quot;
)

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 buildgo 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,这完全取决于你使用的插件。尝试自己运行工具:golintgo vetoracle等,以查看实际的Go警告和错误。

英文:

Relative paths are touchy in Go. For one, I think you need to prefix them with import &quot;./db&quot;. 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(&quot;expected 3, got %d &quot;, result)
    }
 }

Cheers.

huangapple
  • 本文由 发表于 2016年4月15日 12:14:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/36638149.html
匿名

发表评论

匿名网友

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

确定