Wednesday, 1 May 2019

Go Language: Constants


‘const’ keyword is used to define constants.

Example
const (
         name = "Krishna"
         id = 123
         pin = "523169"
)

Above snippet create 3 constants. ‘name’ and ‘pin’ are of type String and ‘id’ is of type integer.

HelloWorld.go
package main

import "fmt"

const (
 name = "Krishna"
 id = 123
 pin = "523169"
)

func main() {

 fmt.Println("name : ", name)
 fmt.Println("id : ", id)
 fmt.Println("pin : ", pin)

}

Output
name :  Krishna
id :  123
pin :  523169

Just like how you define multiple constnats in ‘const’ block, you can define multiple variable in ‘var’ block.


HelloWorld.go
package main

import "fmt"

var (
 name = "Krishna"
 id = 123
 pin = "523169"
)

func main() {

 fmt.Println("name : ", name)
 fmt.Println("id : ", id)
 fmt.Println("pin : ", pin)

}

Output
name :  Krishna
id :  123
pin :  523169


Previous                                                 Next                                                 Home

No comments:

Post a Comment