Today we wrap up our discussion of type identity. This is mostly a recap of what has already been discussed the last few days.
Type identity
…
Given the declarations
type ( A0 = []string A1 = A0 A2 = struct{ a, b int } A3 = int A4 = func(A3, float64) *A0 A5 = func(x int, _ float64) *[]string B0 A0 B1 []string B2 struct{ a, b int } B3 struct{ a, c int } B4 func(int, float64) *B0 B5 func(x int, y float64) *A1 C0 = B0 D0[P1, P2 any] struct{ x P1; y P2 } E0 = D0[int, string] )
these types are identical:
A0, A1, and []string A2 and struct{ a, b int } A3 and int A4, func(int, float64) *[]string, and A5 B0 and C0 D0[int, string] and E0 []int and []int struct{ a, b *B5 } and struct{ a, b *B5 } func(x int, y float64) *[]string, func(int, float64) (result *[]string), and A5
B0
andB1
are different because they are new types created by distinct type definitions;func(int, float64) *B0
andfunc(x int, y float64) *[]string
are different becauseB0
is different from[]string
; andP1
andP2
are different because they are different type parameters.D0[int, string]
andstruct{ x int; y string }
are different because the former is an instantiated defined type while the latter is a type literal (but they are still assignable).
The last sentence is probably the most interesting, because it draws a distinction between type identity and type assignability…. which is the topic we’ll start on tomorrow!
Quotes from The Go Programming Language Specification Version of December 15, 2022