How to declare global variables in Golang.
Any variable declared outside of a function in Go can be accessed by any function within the same package and is referred to as a global variable.
In Go, you can declare a global variable by using the var keyword, followed by the variable name, type, and initial value (if applicable).
For example:
package main
var globalVar int = 42
func main() {
// Access the global variable from within the main function
fmt.Println(globalVar)
}
A global variable named globalVar of type int, initialized with the value 42, has been declared. This variable can be accessed from within the main function.
If a global variable is declared with a lowercase first letter, it will have a limited scope, only being visible within the package it is declared in. However, if it is declared with an uppercase first letter, it will have a wider scope, being visible outside the package as well, as per Go’s scoping rules.