Menu
Coddy logo textTech

Constants

Part of the Fundamentals section of Coddy's GO journey — lesson 12 of 109.

Let's learn about constants in Go. Constants are values that cannot change during program execution.

First, let's declare some constants using the const keyword:

const pi = 3.14
const appName = "Go Calculator"
    
fmt.Println(pi)
fmt.Println(appName)

After executing this code, we'll see the values of our constants displayed on the screen:

3.14
Go Calculator

Unlike variables, constants cannot be changed after they're declared. If you try to assign a new value to a constant, your program will show an error. For example:

const pi = 3.14
pi = 5.6
// Error! because pi is const
challenge icon

Challenge

Beginner
In this challenge, you'll practice using constants in Go. Constants are values that cannot be changed once they're defined.

Your task is to declare a constant named maxStudents with a value of 30, and then print it to the console.

Cheat sheet

Constants are values that cannot change during program execution. Declare constants using the const keyword:

const pi = 3.14
const appName = "Go Calculator"

Constants cannot be reassigned after declaration - attempting to do so will cause an error:

const pi = 3.14
pi = 5.6  // Error! because pi is const

Try it yourself

package main

import "fmt"

func main() {
	// TODO: Declare a constant named maxStudents with value 30
	
	// Print the constant
	fmt.Println("Maximum number of students allowed:", maxStudents)
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals