How can I allow one package access to another package's unexported data only when testing?
问题 In The Go Programming Language , section 11.2.4, there is an example of of an external test accessing fmt.isSpace() via a declaration of IsSpace in fmt 's export_test.go file. That seemed like the perfect solution, so that's what I did: a/a.go : package a var x int func Set(v int) { x = v } a/a_test.go : package a import "testing" func TestSet(t *testing.T) { Set(42) if x != 42 { t.Errorf("x == %d (expected 42)", x) } } func Get() int { return x } (Running go test in a/ works fine.) b/b.go :