Golang package-level variable is “defined outside of any function or method”. A package-level variable is visible to all functions and methods within the package and can be accessed using the variable’s name.
To create a package–level variable in Golang, “declare it outside any function or method”. You can access it anywhere in your package because the scope is bigger and wider now.
Example
package main
import (
"fmt"
)
var data int = 21
func main() {
fmt.Println(data)
}
Output
21
We defined an integer variable “data” at the package level and not inside a function, but we are accessing the “data” variable inside the main() function.
We can access a variable inside the main() function because the variable is package-level and not at any function level. That’s why it is a global variable that can be accessed anywhere in the script.

Krunal Lathiya is a Software Engineer with over eight years of experience. He has developed a strong foundation in computer science principles and a passion for problem-solving. In addition, Krunal has excellent knowledge of Distributed and cloud computing and is an expert in Go Language.