Constant in Golang

Go supports constants of character, string, boolean, and numeric values. And you can not change value of constant.

const declares a constant value

const statement can appear anywhere a var statement can.

With left sample code, it will make error at n = 1, because you can change value of n

Now, please remove line

n = 1

and it run

package main
import "fmt"

const s string = "constant"
func main() {
    fmt.Println(s)
    const n = 500000000
    fmt.Println("constant n = ", n)
    n = 1
}
package main

import "fmt"

const s string = "constant"

func main() {
	fmt.Println(s)
	const n = 500000000
	fmt.Println("constant n = ", n)
	//n = 1
}

View on github


PreNext

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s