Access a Go variable from another package: exports, imports, and globals

Golang global variables across packages: package-level vars with an exported name (capital letter), import path module/subpkg, golang declare global variable in util and read from main; why main cannot be imported and golang package variables vs locals.

Published

Updated

Read time 3 min read

Reviewed byDeepak Prasad

Access a Go variable from another package: exports, imports, and globals

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.DefaultPath is visible from importers.
  • Names starting with lowercase are package-private: util.secret is only for code in package util.
  • package main is not importable from other packages, so you cannot treat main as a library for shared globals. Put shared state in a normal package (for example internal/config or util) and import that from main.

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:

text
go.mod
main.go
util/common.go

util/common.go defines an exported package variable (often what people mean by a golang global variable across packages):

go
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:

go
package main

import (
	"fmt"

	"example.com/demo/util"
)

func main() {
	fmt.Println("The path is:", util.DefaultPath)
}
Output

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


Frequently Asked Questions

1. How do golang global variables across packages work?

Put the variable at package level in a non-main package, give it an exported name starting with a capital letter, then import that package by its module path and refer to pkg.VarName; lowercase names stay internal to the package.

2. What is global variable declaration golang style?

Declare at top level in a file with var Name T = value or var Name T; init and func init can set it; capital N exports it outside the package.

3. Can I read go global var from main in another folder?

Yes if the variable lives in an imported package; you cannot import package main from elsewhere, so globals you need to share must live in a library package, not under package main.

4. Are golang package variables the same as globals in C?

They are package-level shared state visible to all code in that package and, if exported, to importers; they are not process-wide implicit globals without a package qualifier.

5. Why does my import say undefined or not exported?

Either the name starts with a lowercase letter, the import path does not match your module line plus subdirectory, or you are in a different module without a replace directive—see cannot find package and module setup links below.
Deepak Prasad

R&D Engineer

Founder of GoLinuxCloud with more than 15 years of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive …