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 CalculatorUnlike 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 constChallenge
BeginnerYour task is to declare a constant named
maxStudents with a value of 30, and then print it to the console.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)
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Comparison & Logical Operators
Comparison Operators - Part 1Comparison Operators - Part 2Logical AND OperatorLogical OR OperatorLogical NOT OperatorOperator Precedence BasicsRecap - Making Comparisons7Control Flow: Loops
What The `for` Loop ExplainedFor Loop - BasicFor Loop - Condition OnlyThe `break` KeywordThe `continue` KeywordNested LoopsRecap - Repeating Actions2Variables and Basic Data Types
What is a variableType Inference with `:=`Integers (int)Floating-Point NumbersBooleansStringsZero ValuesConstantsNaming ConventionsRecap - Variables and Types5Basic Input/Output
Formatted OutputFormat VerbsPrinting TypesGetting Basic User InputRecap - Input and Output8Functions
Understanding FunctionsDeclaring a FunctionCalling FunctionsFunction ParametersReturning a Single ValueReturning Multiple ValuesNamed Return ValuesFunction Scope BasicsRecap - Creating Reusable Code3Basic Operators
Arithmetic OperatorsDivision OperatorThe Modulo OperatorAssignment OperatorAugmented Assignment OperatorsIncrement and DecrementRecap - Calculations6Control Flow: Conditionals
The `if` StatementThe `else` KeywordThe `else if` KeywordVariable Shadowing in `if`Initializing VariablesThe `switch` StatementSwitch with ExpressionsSwitch without ExpressionThe `fallthrough` KeywordRecap - Making DecisionsPractice on your own: Online Go compiler