Variables and Keywords

Go Keywords

break        default      func         interface    select
case         defer        go           map          struct
chan         else         goto         package      switch
const        fallthrough  if           range        type
continue     for          import       return       var

Declaring & initialize variables

You can declare & initialize variables in many ways ,

package main

import "fmt"

func main() {
    var a int
    var b string
    a = 2
    b = "<b>"

    var c string = "<c>"

    d := 3
    e := "<e>"

    var (
        f = 4                       // f := 4 will cause compile error !
        g = false
    )

    h, i := 20, 16                  // multiple same type variable 
    j := int64(515181632)           // type64
    k := 1.5                        // float64
    var l int                       // value 0 , type int

        m := func() {               // a function can be assign to a variable,
            fmt.Println("hello")    // and this is called anonymous function,
        }                           // m is an address of the function

    fmt.Println(a,b,c,d,e,f,g,h,i,j,k,l,m)

}

[output]
2 <b> <c> 3 <e> 4 false 20 16 515181632 1.5 0 0x204c0

Constants

Constants in Go can only be one of the three types :

  • Numbers
  • Strings
  • Booleans

We can declare the constants in the following ways,

const limit = 641       // constant; type-compatible with any number
const top uint16 = 1234 // constant; type: uint16

const (
    Red = 0
    Yellow = 1
    Blue = 2
)

const (
    Red = iota      // 0
    Yellow          // 1
    Blue            // 2
)

Constants are basically variables whose values cannot be changed later , the example bellow will cause compile error,

package main

import "fmt"

func main() {
    const x string = "Hello World"
    x = "Some other string"
    fmt.Println(x)
}

[output]
prog.go:7: cannot assign to x
 [process exited with non-zero status]

The constant are created at compile time.

Calculations are acceptable, as long as they involve values that are able to be evaluated at compile time, and do not require any runtime activities.

const  myConst  =  4/7

is acceptable, since 4/7 can be evaluated at compile time.

But the bellow is not ,

package main

import "fmt"

func getMyNumber()  int{
    return  5;
}

func main() {
    const  x =  getMyNumber();
    fmt.Println(x)
}

[output]
prog.go:10: const initializer getMyNumber() is not a constant
 [process exited with non-zero status]

Enumeration

In Golang, it is fairly simple to declare enumeration.

package main

import (
    "fmt"
)

type Day int

const (
    Monday Day = iota // starts from 0
    Tuesday
    Wednesday
    Thursday
    Friday
    Saturday
    Sunday
 )

 // [...] is to tell the Go intrepreter/compiler to figure out the array size
 var days = [...]string {"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"}

 func (day Day) String() string {
    return days[day]
 }

func main() {
    fmt.Println(Monday)
    fmt.Println(Tuesday)
}

[output]
Monday
Tuesday

Reference :

Further reference:

  1. Constants - The Go Blog
  2. Go's Declaration Syntax

results matching ""

    No results matching ""