Back to questions
JuniorTheoryCommon
What happens when this program is built and run?
package main
import "fmt"
func main() {
a := 3
b := 1.5
fmt.Println(a + b)
}It does not compile
- Expecting
4.5: Go never converts types implicitly, soa(anint) andb(afloat64) cannot be added until one is converted, as infloat64(a) + b. - Expecting
4or a run-time error: the compiler rejects the mismatch withmismatched types int and float64, so no program is built.
- How would you change the program so that it prints
4.5? - Why does
fmt.Println(3 + 1.5)compile whena + bdoes not?