Golang Tutorial Index


Every programming language requires setting values on memory locations to perform various calculations and logical operations. These values are the constants that are essential in making any program successful. This tutorial will guide you on how to use constants in Go programming.



What Are Constants in Go Programming?

Constants in the Go programming language are fixed values that do not change during program execution once defined. They are either independent values or used as r-values in programming. Any attempt to change a constant value may result in a runtime error in your program.

Constant Declaration and Initialization

In Go programming, the const keyword is used to declare and initialize constants explicitly. Here is a code snippet of declaring and initializing a Constant in Go.

Example:

/* Author: w3schools.in
Description: Example of declaring and initializing a Constant in Go */

package main
import "fmt"

func main() {
    const discount = 40 //The integer value initialized in the Constant discount
    fmt.Println("The assigned value is", discount)
}

Program Output:

The assigned value is 40

Types of Constants in Go

In Go, there are several types of constants. These are:

  1. Numeric constants: These represent a numeric value, such as integers, floating-point numbers, and complex numbers.
  2. Boolean constants: These are constants that represent a boolean value, either true or false.
  3. String constants: These represent a string value enclosed in double quotes, such as "I love go programming".
  4. Rune constants: These constants represent a single Unicode character, written as a single quote followed by the character and a single quote.
  5. Constant expressions: These are expressions that can be evaluated at compile-time to produce a constant value.
  6. Constant enumerations: These are constants that are defined by enumerating a list of constant names and values. For example:
  7. Constant iota: This is a predefined constant used in constant enumerations to represent successive integer values.
  8. Typed constants: These are constants that have a specific type, such as int, float64, or string. Here is an example of how to declare some typed constants in Go:
    // Declare an integer constant with type int
    const Age int = 30
    
    // Declare a floating-point constant with type float64
    const Pi float64 = 3.14159
    
    // Declare a string constant with type string
    const Name string = "John Doe"
    
    // Declare a rune constant with type rune
    const Letter rune = 'A'
    
  9. Untyped constants: These are constants that do not have a type explicitly specified. They can take on the type of context in which they are used. For example:
    const pi = 3.14
    

In Go, constants are defined using the const keyword and must be assigned a value at the time of declaration. Constants cannot be modified after they are created and are evaluated at compile-time rather than runtime.

Here is an example of how to declare some different types of constants in Go:

Example:

// Declare a numeric constant
const PI = 3.14159

// Declare a boolean constant
const IsTrue = true

// Declare a string constant
const Message = "Hello, World!"

// Declare a rune constant
const Letter = 'A'

// Declare a constant expression
const Sum = 3 + 4

Multiple Constant Declaration

Declaring constants in Go can be clubbed into groups, making it easier to declare them all at once. Here is a program on how to do it.

Example:

/* Author: w3schools.in
Description: Example of Go Multiple Constant Declaration */

package main
import "fmt"

const (
    CNAME= "W3schools of Technology"
    TAXID = "AOVLG7802L..."
    STATUS = true
)

func main() {
    fmt.Println(CNAME)
    fmt.Println(TAXID)
    fmt.Println(STATUS)
}

Program Output:

D:\Go-Example>go run filename.go
W3schools of Technology
AOVLG7802L...
true


Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram