
Variables in GoLang
Variables in GoLang
In Go, variables are used to store data that can be manipulated during the execution of a program. Go is a statically typed language, meaning that every variable must have a declared type, and its type cannot be changed once set. Go provides various ways to declare and initialize variables.
Declaring and Initializing Variables
Basic Declaration:Variables can be declared using the
var
keyword, followed by the variable name and the type.Declaration with Initialization:You can also initialize a variable when declaring it.
var name string = "Alice" // Declare and initialize 'name' with "Alice"
Type Inference:Go supports type inference, meaning that if you initialize a variable, Go can automatically infer its type. You don't need to specify the type explicitly.
var name = "Alice" // Go infers that 'name' is of type string
Short Declaration (
:=
):Go also allows short variable declaration within functions, where you don't need to explicitly mention the type. The type is inferred based on the assigned value.name := "Alice" // Go infers 'name' is of type string
This shorthand syntax is only allowed inside functions and is very commonly used in Go.
Types of Variables in Go
Basic Types:
int
,int32
,int64
(signed integer types)uint
,uint32
,uint64
(unsigned integer types)float32
,float64
bool
(boolean values)string
(text data)
Arrays:Go supports arrays that can store multiple values of the same type.
Slices:Slices are more flexible than arrays and allow dynamic resizing.
Structs:Go supports structs, which are collections of fields that can have different types.
Name string Age int}var person Person // Declare a variable of type 'Person'
Maps:Maps are key-value pairs, like dictionaries in Python or hash maps in other languages.
Pointers:Go has pointers, which store the memory address of a variable.
Zero Value
In Go, variables are initialized to zero values if no initial value is assigned. This applies to all types:
- For numeric types (
int
,float64
), the zero value is0
. - For
bool
, the zero value isfalse
. - For
string
, the zero value is""
(empty string). - For arrays, slices, maps, and structs, the zero value is a type-specific default (e.g., an empty slice or map).
var b bool // Zero value is falsevar c string // Zero value is ""
Global Variables
Variables declared outside of functions are global and can be accessed anywhere within the package.
import "fmt"var globalVar = "I'm global"func main() { fmt.Println(globalVar) const Pi = 3.14 // Declare a constant 'Pi' with the value 3.14
Multiple Variable Declaration
Go allows you to declare multiple variables in a single statement, separated by commas.
x, y, z = 10, 20, 30 // Assign values to multiple variables
You can also use short declaration for multiple variables:
Example Code:
import "fmt"func main() { // Declaring and initializing variables var name string = "Alice" var age int = 30 height := 5.6 // Type inferred as float64 isStudent := false // Type inferred as bool // Outputting the values of variables fmt.Println("Name:", name) fmt.Println("Age:", age) fmt.Println("Height:", height) fmt.Println("Is a student:", isStudent) // Modifying a variable's value age = 31 fmt.Println("New age:", age)}
Summary
- Variables in Go are declared using the
var
keyword or shorthand:=
. - Zero values are automatically assigned to variables that are declared but not initialized.
- Go supports basic types, arrays, slices, structs, maps, pointers, and constants.
- Go has strong and static typing, meaning every variable's type must be declared or inferred explicitly.
- Short declaration (
:=
) is commonly used inside functions for quick assignments.
Go's simplicity and explicitness in variable declaration help reduce errors in programs and make the code easier to maintain.