/* * Name equivalence of types and type inference in go * Go essentially does not do type co-ercion * @author gtowell * Created: August 18, 2021 */ package main import "fmt" type bbb int func main() { t1() t2() t4() } // Show that Go uses strict name equivalence for type checking func t1() { var b3 bbb = 16 // set value and explicity declare type fmt.Println(b3) var i3 int = 51 fmt.Println(i3) //i3 = b3 //Will not compiloe -- type checking is name equivalence } // type inference func t2() { b3 := 17 // based on 17, this is of type int. fmt.Println(b3) var i3 int i3 = 51 fmt.Println(i3) i3 = b3 // to make this legal, b3 must be int fmt.Println(i3) var b4 bbb = 68 fmt.Println(b4) //b4 = b3 // Once a type is inferred, it cannot be rethought } // go -- NO type coercion func t3() { var i4 = 14 var i5 = 3.2 fmt.Printf("%v %v\n", i4, i5) //n6 := i4 + i5 // Go will not do type coersion, you must explicitly cast. //fmt.Println(n6) } // passing into function also requires strict name equivalence, here achieved by casting func t4() { t4a := func(aa int, bb int) { fmt.Printf("%v %d\n", aa, bb) } var a16 int16 = 40 var a32 int32 = 48 t4a(int(a16), int(a32)) }