Constants are fixed values that cannot be changed during program execution. They are useful for values that remain the same throughout your code.
Use the const keyword to declare a constant. You must assign a value when declaring it:
const Pi = 3.14159 const Greeting = "Hello, Go!" const IsActive = true
You can declare multiple constants together in a block:
const (
StatusOK = 200
StatusNotFound = 404
StatusError = 500
)
Constants in Go can be typed or untyped. Untyped constants can be assigned to variables of different compatible types.
const UntypedPi = 3.14 // Untyped constant const TypedPi float64 = 3.14 // Typed constant
Click the button below to see what happens if you try to change a constant value in Go:
Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!