Sling Academy
Home/Golang/Working with constants in Go

Working with constants in Go

Last updated: November 23, 2024

Constants are fixed values that do not change during the execution of a program. In Go, constants are defined using the const keyword, and they are typically used for values that should remain consistent across the entire codebase, such as mathematical constants or configuration values.

Basic Usage

Let's go through the basics of declaring and using constants in Go.

package main

import "fmt"

func main() {
    const pi = 3.14159
    fmt.Println("The value of pi is", pi)
}

In this example, we declare a constant pi with the value 3.14159 and print it using fmt.Println.

Typed and Untyped Constants

Go supports both typed and untyped constants. Typed constants are declared with a specific type, whereas untyped constants do not have a specific type until they are used in a context that requires one.

package main

import "fmt"

func main() {
    // Untyped constant
    const answer = 42
    
    // Typed constant
    const typedAnswer int = 42
    
    fmt.Println("Untyped constant:", answer)
    fmt.Println("Typed constant:", typedAnswer)
}

In the code above, answer is untyped, and the type is inferred when needed. typedAnswer, however, is explicitly typed as an int.

Const Grouping

It is possible to group several constants together using parentheses. This can make the code more organized especially when defining related constants.

package main

import "fmt"

func main() {
    const (
        e = 2.71828
        pi = 3.14159
        phi = 1.61803
    )
    fmt.Println("The value of e is", e)
    fmt.Println("The value of pi is", pi)
    fmt.Println("The value of phi is", phi)
}

Using parentheses allows for a cleaner and more organized presentation of related constants.

Advanced Constant Use

In Go, constants can also be defined using expressions as long as all the operands are constants. Let's see an advanced example using such expressions.

package main

import "fmt"

func main() {
    const (
        a = 1
        b = a * 10
        c = b / 2
        result = c + b - a
    )
    fmt.Println("Calculation result:", result)
}

In this example, b, c, and result are computed from the constants using basic arithmetic operations, demonstrating how expressions can be applied to constant values.

String Constants

Constants in Go can also be strings. These are useful for content that does not change during the runtime of a program, such as fixed messages or configuration strings.

package main

import "fmt"

func main() {
    const greeting = "Hello, World!"
    fmt.Println(greeting)
}

This example demonstrates a simple usage of a string constant. The output will be a fixed greeting message.

Conclusion

Constants are an essential feature in Go for managing fixed values that should remain unchanged throughout the program. By understanding how to declare, type, and organize constants, you can write more efficient and maintainable Go programs.

Next Article: Grouped and shorthand declaration in Go

Previous Article: Understanding variable scrope in Go

Series: Variables & Control Flow

Golang

Related Articles

You May Also Like

  • How to remove HTML tags in a string in Go
  • How to remove special characters in a string in Go
  • How to remove consecutive whitespace in a string in Go
  • How to count words and characters in a string in Go
  • Relative imports in Go: Tutorial & Examples
  • How to run Python code with Go
  • How to generate slug from title in Go
  • How to create an XML sitemap in Go
  • How to redirect in Go (301, 302, etc)
  • Using Go with MongoDB: CRUD example
  • Auto deploy Go apps with CI/ CD and GitHub Actions
  • Fixing Go error: method redeclared with different receiver type
  • Fixing Go error: copy argument must have slice type
  • Fixing Go error: attempted to use nil slice
  • Fixing Go error: assignment to constant variable
  • Fixing Go error: cannot compare X (type Y) with Z (type W)
  • Fixing Go error: method has pointer receiver, not called with pointer
  • Fixing Go error: assignment mismatch: X variables but Y values
  • Fixing Go error: array index must be non-negative integer constant