Skip to content

Go: undefined: pkg.name – lowercase names are private

Python & Gotested on Go 1.26.9, macOS (Apple Silicon)

The variable is in package logs, yet the compiler does not know it. In Go the first letter decides visibility: uppercase is exported, lowercase stays inside the package.

Symptom

go build ./cmd/probe
# example.com/logsum/cmd/probe
cmd/probe/main.go:12:19: undefined: logs.combinedRe

The message says undefined; it means: not visible from here.

Diagnosis

The line that accesses the name.

func main() {
	var p logs.Parser = logs.JSON{}
	fmt.Printf("%T\n", p)
	fmt.Println(logs.combinedRe)
}

Causes

combinedReLowercase = unexported

combinedRe is lowercase and therefore invisible outside logs – nobody can swap the regex from outside.

internal/internal/ locks things down further

A foreign module could not even import example.com/logsum/internal/logs: internal/ blocks everything outside example.com/logsum.

Fix

Remove the access (or go through an exported function) and build again.
perl -ni -e 'print unless /combinedRe/' cmd/probe/main.go
go run ./cmd/probe
logs.JSON

Don't: Capitalise everything to make it compile. Every exported name becomes part of the public API.

▶
Break it yourself and find it – free
Go hands-on, Day 2: structs, interfaces and visibility with the logsum tool.
Go to day 2 →

FAQ

What does %T print?

%T prints the dynamic type behind an interface variable, logs.JSON in the course.

Does this apply to fields and methods too?

Yes. Fields, methods, functions and types are only visible outside the package with an uppercase first letter.

Related error patterns

More