forked from steger/pr3-sose2026
30 lines
827 B
Go
30 lines
827 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"math"
|
|
)
|
|
|
|
// Constants are declared like variables, but with the const keyword.
|
|
const s string = "constant"
|
|
|
|
func main() {
|
|
//A constant is a simple unchanging value. Constants can be character, string, boolean, or numeric values.
|
|
fmt.Println(s)
|
|
|
|
//Constants can be declared as a group, like variables.
|
|
const n = 500000000
|
|
|
|
//Constant expressions perform arithmetic with arbitrary precision.
|
|
const d = 3e20 / n
|
|
fmt.Println(d)
|
|
|
|
// A numeric constant has no type until it's given one, such as by an explicit cast.
|
|
fmt.Println(int64(d))
|
|
|
|
// A number can be given a type by using it in a context that requires one,
|
|
// such as a variable assignment or an argument to a function.
|
|
// Here, math.Sin expects a float64, so the untyped constant n is given that type.
|
|
fmt.Println(math.Sin(n))
|
|
}
|