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 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
}

Variable Default Value

When variable is declared but not assigned a value yet, it gets its default value. Different type 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 (name_type1; name_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 , 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. Rune
  8. String as Chars
  9. Variable
  10. Zero Value
  11. Constant
  12. If Then Else
  13. Switch/Case
  14. Loop
  15. Basic Types
  16. Array
  17. Slice
  18. Map
  19. Struct
  20. Function
  21. regexp
  22. Read File
  23. Write to File
  24. Walk Dir
  25. Check File Exist
  26. System Call
  27. Get Script Path
  28. Pointer
  29. Defer
  30. Random Number

Examples

  1. match any regexp
  2. Validate Links
  3. Generate Sitemap

Reference

  1. Go Spec