Welcome to Golang Programs, Get the best new programs in your inbox, daily.

Constants

Go also has support for constants. Constants are essentially variables whose values cannot be changed later. They are created in the same way you create variables, but instead of using the var keyword we use the const keyword:

const x string = "Hello, World"

A const declaration defines named values that look syntactically like variables but whose value is constant, which prevents accidental (or nefarious) changes during program execution.

package main
import "fmt"
const (
        x=10
        y=20
        z=30
    )
func main(){
    const name string ="John Carry" // Constant with data type
    fmt.Println(name)
    const age = 35 // Constant without data type
    fmt.Println(age)
    fmt.Println(x,y,z)
}

The underlying type of every constant is a basic type: boolean, string, or number.