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, “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 seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.