OneBite.Dev - Coding blog in a bite size

declare a global variable in Go

Code snippet on how to declare a global variable in Go

var gVar int  //declaring global variable

This code is used to declare a global variable in Go. The variable is given the type “int”, which stands for integer. In this case the variable is called “gVar”. The keyword “var” lets the compiler know that a new variable is being declared. This variable can now be used throughout the program.

When declaring a global variable, it’s important to make sure it has a unique name that won’t conflict with any other variables that might already exist in the program. That way, the compiler will know exactly which variable is being referred to whenever it appears. The same rules apply if the variable is being declared inside of a function.

go