Concept Flow - Constants
Declare constant with const keyword
Assign fixed value
Use constant in code
Value cannot change
Program runs
Constants are fixed values set once and used throughout the program without change.
package main import "fmt" const Pi = 3.14 func main() { fmt.Println(Pi) }
| Step | Action | Evaluation | Result |
|---|---|---|---|
| 1 | Declare constant Pi | const Pi = 3.14 | Pi is set to 3.14 |
| 2 | Enter main function | func main() | Ready to execute main |
| 3 | Print Pi | fmt.Println(Pi) | Outputs 3.14 |
| 4 | End program | No further code | Program terminates |
| Variable | Start | After 1 | After 2 | Final |
|---|---|---|---|---|
| Pi | undefined | 3.14 | 3.14 | 3.14 |
Constants in Go: - Declared with 'const' keyword - Assigned a fixed value at declaration - Value cannot be changed later - Used like variables but immutable - Helps prevent accidental value changes
const in Go programming?constconst keyword is used to declare constants, which are fixed values that cannot be changed after being set.const declares a value that cannot be changed, while other options describe unrelated concepts.Pi with value 3.14 in Go?const keyword followed by the name and value.const Pi = 3.14, which is the correct syntax. Options A and C declare variables, and B uses invalid Go syntax.package main
import "fmt"
const greeting = "Hello"
func main() {
fmt.Println(greeting)
}greeting is declared with value "Hello" and printed in main.fmt.Println(greeting) prints the string value of the constant, which is "Hello" without quotes.package main
import "fmt"
const x = 10
func main() {
x = 20
fmt.Println(x)
}x as a constant with value 10, then tries to assign 20 to it, which is not allowed.package main
import "fmt"
const (
A = iota
B
C = 5
D
)
func main() {
fmt.Println(A, B, C, D)
}iota starts at 0 and increments by 1 for each line unless overridden.Pass the test and this pattern moves unknown