What is the Package level Variable in Golang

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 packagelevel variable, “declare it outside any function or method”. You can access it anywhere in your package because the scope is bigger and wider now.


package main

import (

var data int = 21

func main() {



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.