[Golang] Constant

2021-09-25 hit count image

Let's see how to define and use Constant in Golang.

Outline

In this blog post, I will show you how to define and use Constant in Golang. If you want to know how to define and use Variable in Golang, see the link below.

You can see the full source code of this blog post on the link below.

Constant

As you know, you can’t change the value of Constant unlike Variable. To see how to use Constant in Golang, create the main.go file and modify it like the below.

package main

import "fmt"

func main() {
    const num int = 10

    fmt.Println(num)
}

In Golang, you can define the Constant with the constant keyword like the below.

// const NAME TYPE = INITIAL_VALUE
const value int = 10

Unlike Variable, Constant can’t omit the initial value. if you omit it, you will get the complie error.

const value int = 10
value = 20 // ERROR

You can’t change the value of Constant initialized once. If you re-assign the value, you will get the complie error.

Type of Constant

The type of Constant also can be omiitted. However, Constant type is determined when it is used unlike variables.

func main() {
    const PI = 3.14

    var a int = PI * 100

    fmt.Println(a)
}

In Golang, we can operate only the same types. So 3.14(float) and 100(int) can’t be operated. But if you define Constant without the type, Constant type is determined when it is used, so the compile error doesn’t occur in this case.

func main() {
    const PI float32 = 3.14

    var a int = PI * 100 // ERROR

    fmt.Println(a)
}

However, if you define Constant with the type like above, the type is different so the compile error occurs.

func main() {
    var PI = 3.14

    var a int = PI * 100 // ERROR

    fmt.Println(a)
}

Variable type is determined when it is defined unlike Constant. So if you define Variable instead of Constant like above, the compile error occurs.

Enum

In Golang, Constant is normally used with Enum type.

const (
    Red   int = 0
    Blue  int = 1
    Green int = 2
)

func main() {
    fmt.Println(Red)
    fmt.Println(Blue)
    fmt.Println(Green)
}

When you execute the code above, you’ll get the result like the below.

0
1
2

iota

In Golang, you can define the Enum type Constant with iota more simply like the below.

const (
    Red int = iota
    Blue int = iota
    Green int = iota
)

func main() {
    fmt.Println(Red)
    fmt.Println(Blue)
    fmt.Println(Green)
}

Repeated types and iota can be ommitted as follows.

const (
    Red int = iota
    Blue
    Green
)

Alos, the type is ommitted, so you can use it like the below.

const (
    Red = iota
    Blue
    Green
)

Completed

Done! we’ve seen how to define Constant and use it in Golang. Also, we’ve seen how to use iota to define the enum type constant.

Was my blog helpful? Please leave a comment at the bottom. it will be a great help to me!

App promotion

You can use the applications that are created by this blog writer Deku.
Deku created the applications with Flutter.

If you have interested, please try to download them for free.

Posts