Golang: Variables

By Xah Lee. Date: . Last updated: .

var declares one or more variables.

var name type → declare a variable.

var name2, name2, name3type → delare variables name1 etc, all of type type.

package main

import "fmt"

var x int
var y float32
var z string

var a, b, c int

func main() {
	fmt.Printf("%#v %#v %#v\n", x, y, z) // 0 0 ""
	// the %#v means show in golang syntax. else, empty string won't be displayed

	fmt.Printf("%#v %#v %#v\n", a, b, c) // 0 0 0
}

Default Value (Zero Value)

When variable is declared but not assigned a value yet, it gets its default value. Different type of value has different default values. The default values are called “zero value

For int type, default is 0.

see Zero Value

Variable with Value

A var declaration can also have value. (golang calls this “initialization”.)

var x = 3

If given a value, then type can be omitted. Golang will infer the type.

package main

import "fmt"

func main() {

	// var declaration, with assignment
	var x = 3
	// no type. golang can infer the type

	fmt.Println(x) // 3

	// print its type
	fmt.Printf("%T\n", x) // int
}

Parallel Assignment

var x, y, z = 3, 4, 1 → delare and assign multiple variables.

package main

import "fmt"

func main() {
	var x, y = 3, 4
	fmt.Println(x, y) // 3 4
}

[see Golang: Basic Types]

Note: it's common to see the following, when a function return multiple values.

var x, y = f(3)

[see Golang: Function]

Variable Scope

package main

import "fmt"

// package level variable
var x = 3

func main() {
	// function level variable
	var y = 4

	fmt.Println(x, y) // 3 4
}

Variable Syntax Shortcut Inside Function

Inside a function, the syntax

name := val

is the same as

var name = val

[see Golang: Function]

package main

import "fmt"

func main() {
	k := 3
	fmt.Println(k)
}

// 3

Grouping Variable Declaration

Alternative syntax for variable is to have var only appear once, like this:

var (name1 type1; name1 type2 etc)

or

var (name1 = val1; name2 = val2 etc)

package main

import "fmt"

var (
	a int
	b = 2
	c = "some"
	d int
)

func main() {
	fmt.Println(a, b, c, d) // 0 2 some 0
}

Note: Semicolon ; is optional when it is at end of line.

There is a command line tool bundled with golang, named “gofmt”, that automatically reformat your go source code to a standard format.

Reference

The Go Programming Language Specification - The Go Programming Language#Variables

If you have a question, put $5 at patreon and message me.

Golang

  1. Compile, Run
  2. Source Encoding
  3. Package, Import
  4. Comment
  5. Print
  6. String
  7. String Functions
  8. Print String
  9. String Backslash Escape
  10. Rune
  11. Variable
  12. Zero Value
  13. Constant
  14. If Then Else
  15. Switch/Case
  16. Loop
  17. Basic Types
  18. Array
  19. Slice
  20. Map
  21. Struct
  22. Function
  23. Closure
  24. Pointer
  25. String, Byte Slice, Rune Slice
  26. regexp
  27. Read File
  28. Write to File
  29. Walk Dir
  30. Check File Exist
  31. System Call
  32. Get Script Path
  33. Defer
  34. Random Number

Examples

  1. Match Any Regexp
  2. Find Replace
  3. Validate Links
  4. Generate Sitemap

Reference

  1. Go Spec