0
0
Goprogramming~5 mins

Constants in Go - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a constant in Go?
A constant is a fixed value that cannot be changed during the program's execution. It is declared using the keyword const.
Click to reveal answer
beginner
How do you declare a constant in Go?
Use the <code>const</code> keyword followed by the name, optional type, and value. Example: <code>const Pi = 3.14</code>
Click to reveal answer
beginner
Can you change the value of a constant after it is declared?
No, constants cannot be changed once declared. Trying to assign a new value will cause a compile-time error.
Click to reveal answer
intermediate
What types of values can constants hold in Go?
Constants can hold boolean, numeric (integer, float, complex), and string values.
Click to reveal answer
beginner
What is the difference between a constant and a variable in Go?
A constant's value is fixed and cannot change, while a variable's value can be changed during program execution.
Click to reveal answer
Which keyword is used to declare a constant in Go?
Aconst
Bvar
Clet
Dstatic
What happens if you try to change the value of a constant after declaration?
AThe value changes successfully
BCompile-time error occurs
CThe program compiles and runs normally
DRuntime error occurs
Which of these can be a constant value in Go?
AA slice
BA struct
CA map
DAn integer
How do you declare multiple constants at once in Go?
AUsing multiple <code>const</code> statements
BUsing <code>var</code> with <code>const</code>
CUsing a <code>const</code> block with parentheses
DUsing <code>let</code> keyword
Which of the following is a valid constant declaration in Go?
Aconst greeting = "Hello"
Bconst number = []int{1,2,3}
Cconst pi = math.Pi
Dconst flag = true
Explain what a constant is in Go and how it differs from a variable.
Think about whether the value can change after declaration.
You got /3 concepts.
    Describe how to declare multiple constants together in Go and give an example.
    Use parentheses after the const keyword.
    You got /2 concepts.