I have a module named “abc”.
go.mod
module abc
go 1.22.2
Then I have a struct that this module exports.
struct.go
package abc
import "fmt"
type A struct {
Left int
Right int
}
func (a A) String() string {
return fmt.Sprintf("%d %d", a.Left, a.Right)
}
And a test for that struct.
abc_test.go
package abc_test
import (
"testing"
abc "abc"
)
func TestOne(t *testing.T) {
a := abc.A{Left: 1, Right: 2}
s := a.String()
if s != Expected() {
t.Errorf("Failed, s is %s", s)
}
}
The function Expected()
is defined in a separate file.
helper.go
package abc_test
func Expected() string {
return "1 2"
}
The issue is file abc_test.go sees the function Expected()
as undefined. I would like to create a function that is only usable inside the test package so that when such a module would be exposed on the internet, the user could not use the helper function for the test directly.
How can I do that?