I'm trying to create random grades and adding them to a test_scores
array. Then calculate the average.
This program:
package main
import (
"fmt"
"math/rand"
)
func main() {
i := 0
var test_scores [5]float64
for i < len(test_scores) {
test_scores[i] = rand.Float64()
i++
}
fmt.Println(test_scores)
var total float64 = 0
i = 0
for i < len(test_scores) {
total += test_scores[i]
i++
}
fmt.Println(total)
fmt.Println(total / len(test_scores))
}
produces:
main.go:24: invalid operation: total / 5 (mismatched types float64 and int)
This one works fine:
package main
import (
"fmt"
"math/rand"
)
func main() {
i := 0
var test_scores [5]float64
for i < len(test_scores) {
test_scores[i] = rand.Float64()
i++
}
fmt.Println(test_scores)
var total float64 = 0
i = 0
for i < len(test_scores) {
total += test_scores[i]
i++
}
fmt.Println(total)
fmt.Println(total / 5)
}
The only difference being that in the final line, I'm using a fixed 5
and on the non-working one, I'm using the len(test_scores)
call.
Len returns an integer as well, so what's up?
When you write 5
directly in the source-code that's called a constant. Same goes for writing true
. The only difference is that the former is an untyped constant and the latter a typed constant.
The difference lies in that there's no ambiguity about what type true
should have – it'll always be bool
but in the case of 5
that's not so obvious and depends on the context.
The Go compiler will figure out what type to give the constant on compilation. The details of this are described in Go's language specification.
Edit:
I realized that there's a mistake in my answer: true
is in fact also untyped according to the spec because it may be utilized anywhere where a type deriving from bool
is expected. That means:
type MyBool bool
func DoNothing(b MyBool) {}
DoNothing(true) // true is coerced to MyBool
The answer is still valid, though. The distinction between typed and untyped constants holds.
float64
and int
are different types, but conversions are allowed under specific circumstances. (http://golang.org/ref/spec#Conversions)
Your literal 5
in the code is an untyped constant (http://golang.org/ref/spec#Constants), and the proper type is determined by the expression during compilation.
Simply use float64(len(test_scores))
This lines
fmt.Printf("%T
", total)
fmt.Printf("%T
", 5)
fmt.Printf("%T
", 5.0)
fmt.Printf("%T
", len(test_scores))
prints
float64
int
float64
int
Maybe compiler perceives 5 as 5.0.. Anyway you should use conversion to float64.