What if changing one number could fix your whole program instantly?
Why Constants in Go? - Purpose & Use Cases
Imagine you are writing a program where you need to use the same value, like the number of days in a week, many times. You type the number 7 everywhere in your code manually.
Typing the same number repeatedly is slow and risky. If you want to change it later, you must find and update every single place. This can cause mistakes and bugs if you miss some spots.
Constants let you give a name to a fixed value once. Then you use that name everywhere. If you need to change the value, you update it in one place only, making your code safer and easier to manage.
daysInWeek := 7 fmt.Println(daysInWeek) // Later, if days change, you must update all 7s manually
const daysInWeek = 7 fmt.Println(daysInWeek) // Change daysInWeek once if needed, all uses update automatically
Constants make your code clear, reliable, and easy to update by naming fixed values just once.
Think of a recipe that always uses 2 cups of sugar. Instead of writing '2' everywhere, you name it 'sugarAmount'. If the recipe changes, you update 'sugarAmount' once, and the whole recipe stays correct.
Constants store fixed values with meaningful names.
They prevent errors from repeated manual typing.
Updating constants is simple and safe.