Declaration in Go

Go: Declaration

Variable declaration in Golang Type goes after identifier

Here is an example -

    var foo int // declaration without initialization
    var foo int = 42 // declaration with initialization
    var foo, bar int = 42, 1302 // declare and init multiple vars at once
    var foo = 42 // type omitted, will be inferred
    foo := 42 // shorthand, only in func bodies, omit var keyword, type is always implicit
    const constant = "This is a constant"

    //iota can be used for incrementing numbers, starting from 0
    const (
        _ = iota
        a
        b
        c = 1 << iota
        d
    )
    fmt.Println(a, b) // 1 2 (0 is skipped)
    fmt.Println(c, d) // 8 16 (2^3, 2^4)

Previous Article

Next Article