Go errors

Go (Golang) compile errors, runtime panics and module problems with idiomatic fixes.

CodeErrorWhat it means
invalid memory address or nil pointer dereference Go: panic: runtime error: invalid memory address or nil pointer dereference The program used a pointer, map, interface or struct field that was nil — usually an uninitialised value or a…
panic: close of closed channel Go: panic: close of closed channel (or close of nil channel) `close(ch)` was called twice, or on a nil channel.
no new variables on left side of := Go: ./main.go:9:6: no new variables on left side of := `:=` declares variables, but all names on the left already exist in the same scope.
missing return Go: ./main.go:18:1: missing return A function with a return type can reach its end without a return statement on some path.
json: cannot unmarshal string into Go struct field Go: json: cannot unmarshal string into Go struct field User.age of type int The JSON value's type does not match the Go field type (string vs number, object vs array).
all goroutines are asleep - deadlock Go: fatal error: all goroutines are asleep - deadlock! Every goroutine is blocked waiting (on a channel, mutex or WaitGroup), so the program can never continue.
go: updates to go.mod needed Go: go: updates to go.mod needed; to update it: go mod tidy `go.mod` is out of sync with the imports in your code, and Go is running with `-mod=readonly` (the default).
missing go.sum entry Go: missing go.sum entry for module providing package github.com/pkg/errors (imported by app); to add: go get app `go.sum` lacks the checksum for a dependency that your build needs.
reflect: call of reflect.Value.X on zero Value Go: panic: reflect: call of reflect.Value.Elem on zero Value Reflection code operated on an invalid (zero) `reflect.Value`, often derived from a nil pointer or a missing…
panic: test timed out after 10m0s Go: panic: test timed out after 10m0s — running tests: TestFoo (10m0s) `go test` killed the test binary because it exceeded the timeout (default 10 minutes), usually a hung test.
does not implement interface (missing method) Go: cannot use &Dog{} (value of type *Dog) as Animal value: *Dog does not implement Animal (missing method Speak) A type was used as an interface but lacks a method or has it with a different signature or receiver type.
fatal error: concurrent map writes Go: fatal error: concurrent map writes Two goroutines wrote to the same map at the same time; the runtime detects it and aborts the program (not rec…
not enough / too many arguments in call Go: not enough arguments in call to f have (int) want (int, string) A function was called with fewer or more arguments than it declares.
x509: certificate is valid for X, not Y Go: x509: certificate is valid for www.example.com, not api.example.com The server's certificate does not list the hostname you connected to (SAN mismatch), or it has expired.
undefined: x Go: ./main.go:12:2: undefined: handler The compiler cannot find an identifier — a typo, a missing import, or it lives in another file/package that w…
declared and not used Go: declared and not used: x / "fmt" imported and not used Go refuses to compile code with unused local variables or unused imports.
no Go files in directory Go: package app/cmd: no Go files in /app/cmd / build constraints exclude all Go files The directory you asked Go to build contains no buildable `.go` files.
panic: assignment to entry in nil map Go: panic: assignment to entry in nil map You wrote to a map that was declared but never initialised.
fatal error: stack overflow Go: runtime: goroutine stack exceeds 1000000000-byte limit — fatal error: stack overflow Unbounded recursion consumed the goroutine's stack until it hit the 1 GB limit.
assignment mismatch: 1 variable but f returns 2 values Go: assignment mismatch: 1 variable but os.Open returns 2 values The number of variables on the left does not match the number of values the function returns.
cannot assign to struct field m[k].f in map Go: cannot assign to struct field users["bob"].Age in map Map values are not addressable, so you cannot modify a field of a struct stored directly in a map.
dial tcp: connect: connection refused Go: dial tcp 127.0.0.1:5432: connect: connection refused Nothing is listening on the host and port your Go program tried to connect to.
context deadline exceeded Go: Get "https://api.example.com": context deadline exceeded (Client.Timeout exceeded while awaiting headers) An operation did not finish before its context deadline or HTTP client timeout.
invalid character looking for beginning of value Go: invalid character '<' looking for beginning of value The decoder received something that is not JSON — often an HTML error page, an empty body or plain text.
invalid operation: mismatched types Go: invalid operation: a + b (mismatched types int and float64) You combined two values of different types; Go never converts numeric types implicitly.
no required module provides package Go: no required module provides package github.com/foo/bar; to add it: go get github.com/foo/bar The code imports a package whose module is not listed in `go.mod`.
import cycle not allowed Go: package app/models imports app/utils imports app/models: import cycle not allowed Two or more packages import each other directly or indirectly, which Go forbids.
panic: slice bounds out of range Go: panic: runtime error: slice bounds out of range [:5] with capacity 3 A slice expression `s[a:b]` used bounds outside the slice's length or capacity.
function main is undeclared in the main package Go: runtime.main_main·f: function main is undeclared in the main package The package is named `main` but contains no `func main()`, or you tried to build a library as an executable.
sql: database is closed / driver: bad connection Go: sql: database is closed The `*sql.DB` handle was closed (or the connection died) before the query ran.
x509: certificate signed by unknown authority Go: x509: certificate signed by unknown authority The Go TLS client does not trust the CA that signed the server's certificate.
imported and not used Go: ./main.go:5:2: "os" imported and not used The file imports a package that no code in that file uses; Go treats this as a compile error, not a warning.
cannot use x as type in assignment/argument Go: cannot use "5" (untyped string constant) as int value in argument to f A value's type is not assignable to the type required by the variable, parameter or return value.
context canceled Go: context canceled (rpc error: code = Canceled desc = context canceled) The context was cancelled before the operation finished — the caller gave up, the client disconnected, or a p…
fatal error: concurrent map read and map write Go: fatal error: concurrent map read and map write One goroutine read from a map while another wrote to it; the runtime aborts.
terminal prompts disabled / 410 Gone (private module) Go: fatal: could not read Username for 'https://github.com': terminal prompts disabled Go tried to fetch a private repository without credentials (and, via the public proxy, got 404/410).
invalid version: unknown revision Go: go: github.com/foo/bar@v1.2.3: invalid version: unknown revision v1.2.3 Go cannot fetch the requested module version — the tag or branch doesn't exist, the repository is private or…
http: superfluous response.WriteHeader call Go: http: superfluous response.WriteHeader call from main.handler (main.go:25) A handler wrote the status header more than once, or wrote the body first (which sends 200) and then tried to…
listen tcp :8080: bind: address already in use Go: listen tcp :8080: bind: address already in use The port your Go server wants is already taken by another process (or a previous instance of your app).
Client.Timeout exceeded while awaiting headers Go: net/http: request canceled (Client.Timeout exceeded while awaiting headers) An `http.Client` with a Timeout gave up waiting for the server's response headers.
panic: send on closed channel Go: panic: send on closed channel A goroutine sent a value to a channel after it had been closed.
sql: no rows in result set Go: sql: no rows in result set `QueryRow(...).Scan(...)` found no row for the query.
WARNING: DATA RACE Go: WARNING: DATA RACE — Write at 0x00c0000 by goroutine 7: / Previous read at ... by goroutine 6 The race detector (`-race`) observed two goroutines accessing the same memory concurrently with at least one…
too many open files Go: accept tcp [::]:8080: accept4: too many open files / open x: too many open files The process reached its file-descriptor limit — usually leaked connections, files or response bodies.
cannot find package in any of GOROOT / GOPATH Go: cannot find package "github.com/pkg/errors" in any of: /usr/local/go/src/github.com/pkg/errors (from $GOROOT) /go/src/github.com/pkg/errors (from $GOPATH) Go could not locate an imported package — it is not downloaded, the module is not initialised, or GOPATH mode…
cannot refer to unexported name Go: cannot refer to unexported name pkg.helper (undefined: pkg.helper) You used a lowercase (unexported) identifier from another package; only capitalised names are visible outside…
cgo: C compiler gcc not found Go: cgo: C compiler "gcc" not found: exec: "gcc": executable file not found in $PATH A package using cgo needs a C compiler, but none is installed or CGO is disabled.
panic: interface conversion Go: panic: interface conversion: interface {} is string, not int A type assertion `x.(T)` failed because the dynamic type of the interface is not T.
panic: sync: negative WaitGroup counter Go: panic: sync: negative WaitGroup counter `wg.Done()` was called more times than `wg.Add()`.
verifying module: checksum mismatch / SECURITY ERROR Go: verifying module: checksum mismatch — SECURITY ERROR This download does NOT match an earlier download recorded in go.sum A module's content hash differs from the one recorded in `go.sum` (or the public checksum database).
syntax error: unexpected newline Go: ./main.go:8:2: syntax error: unexpected newline, expected comma or ) The parser met a line break where it needed more tokens, usually a missing trailing comma in a multi-line lit…
panic: runtime error: index out of range Go: panic: runtime error: index out of range [3] with length 3 Code accessed a slice, array or string element beyond its length.
panic: runtime error: integer divide by zero Go: panic: runtime error: integer divide by zero Integer division or modulo by zero.