People ask about golang global variables across packages, golang package variables, or go global var when wiring a small module: in Go there is no global keyword. You use package-level variables (declared outside func in a file). Another package can read or write them only if the name is exported (starts with an uppercase letter) and you import the defining package by its module path. For more on lifecycle and patterns, see golang global variables; for := vs var and block scope, see variable scope in Go; for modules layout, see create a Go module.
Tested with Go 1.24 on Linux.
Export rules (what another package may see)
- Names starting with an uppercase letter are exported:
util.DefaultPathis visible from importers. - Names starting with lowercase are package-private:
util.secretis only for code inpackage util. package mainis not importable from other packages, so you cannot treatmainas a library for shared globals. Put shared state in a normal package (for exampleinternal/configorutil) and import that frommain.
That matches searches like golang declare global variable and golang globals: they are normal var declarations at file top level with the right capitalization.
Example: util package and main
Assume a module example.com/demo with this layout:
go.mod
main.go
util/common.goutil/common.go defines an exported package variable (often what people mean by a golang global variable across packages):
package util
var DefaultPath string
func init() {
DefaultPath = "/tmp"
}main.go imports the path example.com/demo/util (module path + /util) and uses the qualified name:
package main
import (
"fmt"
"example.com/demo/util"
)
func main() {
fmt.Println("The path is:", util.DefaultPath)
}You should see The path is: /tmp after go run . from the module root. Use go mod init example.com/demo once; see modules tutorial if go.mod is new.
Why you cannot import variables “from main” downward
Go forbids import cycles. package main is the program entry, not a reusable import path, so child packages must not import main. If you need shared configuration, define golang package variables (exported or not) in a small library package both main and other subpackages import. If imports fail with “cannot find package” or “not exported,” align module in go.mod with your import strings—cannot find package / GOROOT walks through typical mistakes.
Summary
Golang global variable across packages really means a package-level var with an exported identifier in a non-main package, plus a correct module import path. Lowercase names stay inside the package; main is not a library. Pair this article with golang global variables for initialization order and best practices, and variable scope for locals versus package block.
References
- The Go Programming Language Specification — Exported identifiers
- Organizing a Go module
- Variable scope in Go
- Golang global variables tutorial

