Type identities of other types

May 30, 2023

Type identity

  • Two pointer types are identical if they have identical base types.

Given:

type (
	P = *int
	Q = P
	R *int
)

P, Q, and *int are all identical to each other, but different from *int32 and R.

  • Two function types are identical if they have the same number of parameters and result values, corresponding parameter and result types are identical, and either both functions are variadic or neither is. Parameter and result names are not required to match.

Given:

type (
	F1 = func(x, y int) error
	F2 = func(int, int) error
	F3 = func([]int)
	F4 = func(...int)
)
  • F1 and F2 are identical (parameter and result names are not required to match)
  • F3 and F4 are different, because only one is variadic, even though the number and type of parameters is otherwise the same.
  • Two interface types are identical if they define the same type set.

Notably, this differs from struct identity, in that the order of method declaration is unimportant. Therefore, given:

type (
	I1 = interface {
		Foo()
		Bar()
	}
  I2 = interface {
		Bar()
		Foo()
	}
)

I1 and I2 are identical, becasue they define the same type set.

  • Two channel types are identical if they have identical element types and the same direction.

Given:

type (
	C1 = chan int
  C2 = chan<- int
  C3 = <-chan int
)

C1, C2, and C3 are all different from each other, even though, interestingly, they may represent the exact same value!

  • Two instantiated types are identical if their defined types and all type arguments are identical.

Quotes from The Go Programming Language Specification Version of December 15, 2022


Share this

Direct to your inbox, daily. I respect your privacy .

Unsure? Browse the archive .

Related Content


Type identities of compound types

So we now know that types are either identical or different. let’s look at the details, with examples. Type identity … In detail: Two array types are identical if they have identical element types and the same array length. So given: type ( X = [10]int Y = X Z [10]int ) Types X, Y, and [10]int are identical to each other, but not to [9]int or [10]int32. Type Z is also different from all the others, due to the fact that it is a named type.


Type identity

Type identity Two types are either identical or different. Well now that’s profound, isn’t it? LOL. The real point is that there are not different “degrees of similarity.” The spec continues… A named type is always different from any other type. Otherwise, two types are identical if their underlying type literals are structurally equivalent; that is, they have the same literal structure and corresponding components have identical types. So the rules for type identity are rather simple.


Go statements, conclusion

Today we finish the description of go statements: Go statements … When the function terminates, its goroutine also terminates. If the function has any return values, they are discarded when the function completes. go Server() go func(ch chan<- bool) { for { sleep(10); ch <- true }} (c) Okay, so that bit about discarding return values makes sense, right? func main() { go sum(1, 3) // return value discarded } func sum(a, b int) int { return a + b } But what if you need that return value for something?

Get daily content like this in your inbox!

Subscribe