Go code
const daysInWeek = 7
fmt.Println(daysInWeek)Line-by-line explanation
constcreates an unchangeable named value.daysInWeekis the name.7is the value.- Println displays it.
Expected output
7The constant can be read like a variable.
Constants work with values the compiler can know directly, such as numbers, strings, and booleans. They are useful for facts and fixed settings in code.
Broken Go code
const daysInWeek = 7
daysInWeek = 8Line-by-line explanation
- The first line creates the constant.
- The second line tries to reassign it.
- The compiler rejects the assignment.
Expected output
cannot assign to daysInWeekThe compiler protects the constant from change.