Ktoś byłby w stanie wyjaśnić skąd się biorą różnice? Po uruchomieniu kodu:
package main
import (
"fmt"
"math"
)
func main() {
const (
a = 4020.02839 * iota
b
c
)
fmt.Printf("type(a) = %t\n", a)
fmt.Printf("type(b) = %t\n", b)
fmt.Printf("type(c) = %t\n", c)
d := math.Pow(2, 3)
fmt.Printf("type(d) = %t\n", d)
}
Otrzymujemy output:
type(a) = %!t(float64=0)
type(b) = %!t(float64=4020.02839)
type(c) = %!t(float64=8040.05678)
type(d) = %!t(float64=8)
Natomiast jak się użyje potęgowania wraz z iota, to dostajemy błąd kompilacji:
package main
import (
"fmt"
"math"
)
func main() {
const (
a = math.Pow(2, iota)
b
c
)
fmt.Printf("type(a) = %t\n", a)
fmt.Printf("type(b) = %t\n", b)
fmt.Printf("type(c) = %t\n", c)
}
Szczerze mówiąc ten błąd mi niewiele pomaga:
# command-line-arguments
./const_bytes.go:10:7: math.Pow(2, iota) (value of type float64) is not constant
./const_bytes.go:11:3: math.Pow(2, iota) (value of type float64) is not constant
./const_bytes.go:12:3: math.Pow(2, iota) (value of type float64) is not constant
Wcześniej również mieliśmy float64 jako constant i działało.