Variables are containers that store data values. In Go, you declare variables with the var
keyword and optionally initialize them. Golang is statically typed, meaning you must specify or infer the data type.
package main import "fmt" func main() { var name string = "Technorank" var age int = 25 fmt.Println("Name:", name) fmt.Println("Age:", age) }
Output:
Name: Technorank Age: 25
If you donβt assign a value at the time of declaration, Go assigns a zero value (like 0
, ""
, or false
).
var count int // count will be 0 by default fmt.Println(count)
You can use :=
to declare and assign in one step, only inside functions.
message := "Hello Go!" fmt.Println(message)
:=
only inside functions. Outside functions, you must use var
.
var a, b, c = 1, 2, 3 x, y := "Go", true fmt.Println(a, b, c) fmt.Println(x, y)
int
β for integersfloat64
β for floating numbersstring
β for textbool
β for true/false valuesVariables can change. Constants (const
) can't.
const pi = 3.14 fmt.Println(pi)
var
to declare variables with or without initialization.:=
for short declarations inside functions.Try the examples on Go Playground
Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!