Follow

Follow
Go: Define Constants

Go: Define Constants

Elucian Moise's photo
Elucian Moise
Β·Mar 24, 2023Β·

1 min read

In Go it is good to use constants instead of regular variables to define identifiers for values that will not change during program execution. We use keyword "const" to define constants.

package main

import "fmt"

// global public constant
const Pi = 3.14

func main() {
    //local string constant
    const world = "δΈ–η•Œ"
    fmt.Println("Hello", world)
    fmt.Println("Happy", Pi, "Day")

    //local Boolean constant
    const truth = true
    fmt.Println("Go rules?", truth)
}

Try it: https://go.dev/play/p/9mY4cwCCbDP

Notes:


Constants can be global or local. Usually global constants start with capital letters to be public. Local constants make sense to define with lowercase, but you can also use uppercase letters.

  • Constants are declared with the const keyword.

  • Constants can be character, string, boolean, or numeric values.

  • Constants must be initialized with literal using the assign symbol "=".


If you are geting older, increase the spead! πŸ––πŸΌπŸ€

Β 
Share this