Variables
What is a variable?
a variable is a named storage location that contains a value. For example, the following code declares a variable named x
that contains the value 1
:
Declaring variables
Variables are declared using the var
, const
, and the walrus operator :=
. The syntax for declaring a variable is as follows:
function scoped variables
a function scoped variable is a variable that is only accessible within the function it is declared in. The syntax for declaring a function scoped variable is as follows:
main.go | |
---|---|
package scoped variables
a package scoped variable is a variable that is accessible within the package it is declared in. The syntax for declaring a package scoped variable is as follows:
main.go | |
---|---|
global scoped variables
a global scoped variable is a variable that is accessible within the entire program. The syntax for declaring a global scoped variable is as follows:
main.go | |
---|---|
Exporting variables requires you to capitalize the first letter of the variable name. if you want to keep the variable only accessible within the package, you can keep the first letter lowercase.