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 …)

or

var (name1 = val1; name2 = val2 …)

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. Print String
  8. String Backslash Escape
  9. Rune
  10. Variable
  11. Zero Value
  12. Constant
  13. If Then Else
  14. Switch/Case
  15. Loop
  16. Basic Types
  17. Array
  18. Slice
  19. Map
  20. Struct
  21. Function
  22. Closure
  23. Pointer
  24. String, Byte Slice, Rune Slice
  25. regexp
  26. Read File
  27. Write to File
  28. Walk Dir
  29. Check File Exist
  30. System Call
  31. Get Script Path
  32. Defer
  33. Random Number

Examples

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

Reference

  1. Go Spec