Constants in Go are used to declare values that cannot be changed during the execution of a program. Constants are declared using the const
keyword, followed by the name of the constant, the type, and the value.
Here’s an example of declaring a constant in Go:
package main
import "fmt"
func main() {
const Pi float64 = 3.14159265358979323846
fmt.Println("Pi:", Pi)
}
In the above example, Pi
is a constant of type float64
with the value 3.14159265358979323846
.
Constants can also be declared without specifying a type, in which case the type is inferred from the value. Here’s an example:
package main
import "fmt"
func main() {
const Pi = 3.14159265358979323846
fmt.Println("Pi:", Pi)
}
In this tutorial, we’ve covered how to declare constants in Go using the const
keyword. Constants are used to declare values that cannot be changed during the execution of a program.
Leave a Reply