Introduction
This lab aims to test your understanding of constants in Golang.
This lab aims to test your understanding of constants in Golang.
The problem to be solved is to demonstrate the use of constants in Golang for character, string, boolean, and numeric values.
The lab has the following requirements:
const
keyword to declare a constant value.var
statement can.$ go run constant.go
constant
6e+11
600000000000
-0.28470407323754404
There is the full code below:
// Go supports _constants_ of character, string, boolean,
// and numeric values.
package main
import (
"fmt"
"math"
)
// `const` declares a constant value.
const s string = "constant"
func main() {
fmt.Println(s)
// A `const` statement can appear anywhere a `var`
// statement can.
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 conversion.
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 function call. For example, here
// `math.Sin` expects a `float64`.
fmt.Println(math.Sin(n))
}
In this lab, you learned how to declare and use constants in Golang. Constants can be of character, string, boolean, and numeric values. Constant expressions perform arithmetic with arbitrary precision. A numeric constant has no type until it's given one, such as by an explicit conversion.