ARTICLE AD BOX
I am trying to write an example test function in go, but when I run the test with go test I receive this error. My functions follow the naming pattern ExampleFunction, so I am lost as to why the unknown identifier is present.
# github.com/learngo/cmd/hello # [github.com/learngo/cmd/hello] cmd/hello/main_test.go:5:1: ExampleGreet refers to unknown identifier: Greet cmd/hello/main_test.go:12:1: ExampleDouble refers to unknown identifier: Double FAIL github.com/learngo/cmd/hello [build failed] FAILMy main.go file:
package main func double(x int) int { doubled := x * 2 return doubled } func greet(name string) string { return "Hello " + name + "." } func main() { fmt.Println(greet("friend")) fmt.Println(double(3)) }My main_test.go file:
package main import "fmt" func ExampleGreet() { fmt.Println(greet("World")) // Output: // Hello World. } func ExampleDouble() { fmt.Println(double(2)) fmt.Println(double(9)) // Output: // 4 // 18 }my go env configuration:
AR='ar' CC='gcc' CGO_CFLAGS='-O2 -g' CGO_CPPFLAGS='' CGO_CXXFLAGS='-O2 -g' CGO_ENABLED='0' CGO_FFLAGS='-O2 -g' CGO_LDFLAGS='-O2 -g' CXX='g++' GCCGO='gccgo' GO111MODULE='' GOAMD64='v1' GOARCH='amd64' GOAUTH='netrc' GOBIN='/home/ubuntu/.local/bin' GOCACHE='/home/ubuntu/.cache/go-build' GOCACHEPROG='' GODEBUG='' GOENV='/home/ubuntu/.config/go/env' GOEXE='' GOEXPERIMENT='' GOFIPS140='off' GOFLAGS='' GOGCCFLAGS='-fPIC -m64 -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build2023379519=/tmp/go-build -gno-record-gcc-switches' GOHOSTARCH='amd64' GOHOSTOS='linux' GOINSECURE='' GOMOD='/home/ubuntu/repos/github.com/robbiestoffel/learngo/go.mod' GOMODCACHE='/home/ubuntu/.local/share/go/pkg/mod' GONOPROXY='' GONOSUMDB='' GOOS='linux' GOPATH='/home/ubuntu/.local/share/go' GOPRIVATE='' GOPROXY='direct' GOROOT='/home/ubuntu/.local/go' GOSUMDB='sum.golang.org' GOTELEMETRY='local' GOTELEMETRYDIR='/home/ubuntu/.config/go/telemetry' GOTMPDIR='' GOTOOLCHAIN='auto' GOTOOLDIR='/home/ubuntu/.local/go/pkg/tool/linux_amd64' GOVCS='' GOVERSION='go1.25.7' GOWORK='' PKG_CONFIG='pkg-config'my go.mod
module github.com/learngo go 1.25.7my tree:
. ├── cmd │ └── hello │ ├── main.go │ └── main_test.go ├── go.mod ├── LICENSE ├── pkg └── README.md 4 directories, 5 files