Tsallaka zuwa abun ciki
Justice Tech School

Declare Variables

var name type = value is the full form. The type can be left out when a value is given. Inside functions, := declares and assigns in one step. Declare several at once with commas or a var block.

Example
var a int = 5
var b = "text"
c := 3.5
var x, y = 1, 2
var (
	name string = "Ada"
	age  int    = 21
)
fmt.Println(a, b, c, x, y, name, age)

Result

5 text 3.5 1 2 Ada 21

Exercise

Declare and assign inside a function.

n  10

Quiz Test

Can := be used outside functions?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.