Golang Tutorial
Fundamentals
Control Statements
Functions & Methods
Structure
Arrays & Slices
String
Pointers
Interfaces
Concurrency
Variables are essential in any programming language, and Go is no exception. In this tutorial, we'll explore variables in Go, including how to declare, initialize, and use them.
In Go, a variable is a name given to a storage area that programs can manipulate. Each variable in Go has a specific type, which determines the size and layout of the variable's memory.
var variableName type
Example:
var age int
var variable1, variable2 type
Example:
var height, width int
var variableName type = value
Example:
var name string = "Alice"
var city = "New York"
:=
shorthand to declare and initialize a variable.country := "USA"
var ( a int b float64 c string )
Variables declared without an explicit initial value are given their zero value. The zero value is:
0
for numeric types.false
for the boolean type.""
(empty string) for strings.In Go, the scope of a variable refers to where you can access that variable. Variables can be scoped at:
Function Level (Local Variables): Declared inside a function and can't be accessed outside that function.
Package Level: Declared outside all functions and are accessible throughout the entire package.
Global Level: Declared with a capital first letter, making them exported and accessible outside the package.
You can perform operations on variables depending on their data type. For instance, with integers, you can perform arithmetic operations:
var a = 5 var b = 3 result := a + b
Once a variable is declared, you can change its value (unless it's declared as const
).
var message = "Hello" message = "Hi"
Variables are foundational in Go, as in most programming languages. Properly declaring, initializing, and understanding the scope of variables is crucial when developing Go applications. With Go's strong and static type system, it's essential to ensure that you are always aware of the type of each variable and how it interacts within your program.
Declaration and initialization of variables in Golang:
var
keyword, and the type can be explicitly mentioned or left for the compiler to infer.package main import "fmt" func main() { var number int number = 42 // Alternatively, declare and initialize in one line var message string = "Hello, Golang!" fmt.Println(number) fmt.Println(message) }
Variable types in Golang:
package main import "fmt" func main() { // Built-in types var age int = 25 var pi float64 = 3.14 var name string = "John" var isStudent bool = true // User-defined type type customType int var customVar customType = 100 fmt.Println(age, pi, name, isStudent, customVar) }
Scope and lifetime of variables in Golang:
package main import "fmt" var globalVar = "I am global" func main() { var localVar = "I am local" fmt.Println(localVar) // Accessible within the main function exampleFunction() } func exampleFunction() { // fmt.Println(localVar) // This would result in an error as localVar is not accessible here fmt.Println(globalVar) // Accessible as a global variable }
Constants vs variables in Golang:
const
keyword.package main import "fmt" func main() { const pi = 3.14 // pi = 3.1415 // This line would result in an error fmt.Println(pi) }
Golang variable naming conventions:
package main import "fmt" func main() { var myVariable int anotherVariable := "I am another variable" fmt.Println(myVariable, anotherVariable) }
Pointers and reference variables in Golang:
*
symbol.package main import "fmt" func main() { var num = 42 var pointer *int = &num fmt.Println("Value:", num) fmt.Println("Address:", &num) fmt.Println("Value via pointer:", *pointer) }
Type inference in Golang variables:
package main import "fmt" func main() { var age = 25 // Type inferred as int var name = "John" // Type inferred as string fmt.Println(age, name) }