Go Type Alias
Take a look at type intList = []int
in the code below. That’s an
example of a Type Alias. Specifically, it’s a Type Alias for []int
.
Okay, so now take a look at type Name1 []int
, which is a named type …
that’s different.
package main
import (
"fmt"
"reflect"
)
type intList = []int
// Here, forced to same type
func coerce(a intList, b intList) bool {
if reflect.DeepEqual(b, a) {
fmt.Printf("equal\n")
return true
}
fmt.Printf("Not equal\n")
return false
}
func main() {
type Name1 []int
a := intList{}
a = append(a, 3)
b := []int{}
b = append(b, 3)
f := Name1{}
f = append(f, 3)
coerce(f, b)
// Above is equal
// Below we expect this to be equal, and it is.
if reflect.DeepEqual(b, a) {
fmt.Printf("yes..equal\n")
} else {
fmt.Printf("not equal..\n")
}
// This is not equal
if reflect.DeepEqual(f, a) {
fmt.Printf("Never get here ...\n")
} else {
fmt.Printf("not equal..\n")
}
}
Output:
equal yes..equal not equal..
References: