Go Type Assertions: Difference between revisions
Jump to navigation
Jump to search
(Created page with "=External= * https://go.dev/ref/spec#Type_assertions =Internal= * Go Language * Go Interfaces =Overview= An i...") |
|||
(8 intermediate revisions by the same user not shown) | |||
Line 2: | Line 2: | ||
* https://go.dev/ref/spec#Type_assertions | * https://go.dev/ref/spec#Type_assertions | ||
=Internal= | =Internal= | ||
* [[ | * [[Go_Language#Type_Assertions|Go Language]] | ||
* [[Go Interfaces# | * [[Go Interfaces#Type_Assertions|Go Interfaces]] | ||
=Overview= | =Overview= | ||
A type assertion is an expression that probes whether an interface is of a certain concrete type, and if it is, returns a variable of that type. | |||
<syntaxhighlight lang='go'> | <syntaxhighlight lang='go'> | ||
var i SomeInterface | var i SomeInterface | ||
v, | v, isTypeName := i.(TypeName) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
An interface hides the differences between the types implementing it and emphasizes the commonality. However, there are times you may want to know what exact concrete type exists behind the interface. Type assertions can be used for type disambiguation. | |||
An interface hides the differences between the types implementing it and emphasizes the commonality. | |||
However, there are times you may want to know what exact concrete type exists behind the interface. | |||
Type assertions can be used for type disambiguation | |||
<font color=darkkhaki>Actually, it worked with: | |||
<syntaxhighlight lang='go'> | <syntaxhighlight lang='go'> | ||
v, isTypeName := i.(*TypeName) | |||
v, | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Why is that? Probably because the interface variable contained a pointer to the actual type. | |||
</font> | |||
Usage example: | Usage example: | ||
Line 116: | Line 57: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=Type Assertion= | |||
=Type Switch= | |||
Type assertion with <code>switch</code>: | Type assertion with <code>switch</code>: | ||
<syntaxhighlight lang='go'> | <syntaxhighlight lang='go'> |
Latest revision as of 20:48, 9 May 2024
External
Internal
Overview
A type assertion is an expression that probes whether an interface is of a certain concrete type, and if it is, returns a variable of that type.
var i SomeInterface
v, isTypeName := i.(TypeName)
An interface hides the differences between the types implementing it and emphasizes the commonality. However, there are times you may want to know what exact concrete type exists behind the interface. Type assertions can be used for type disambiguation.
Actually, it worked with:
v, isTypeName := i.(*TypeName)
Why is that? Probably because the interface variable contained a pointer to the actual type.
Usage example:
type SomeInterface interface {
MethodA()
}
type TypeA struct {
s string
}
func (v *TypeA) MethodA() {
fmt.Println("TypeA.MethodA()")
}
type TypeB struct {
s string
}
func (v *TypeB) MethodA() {
fmt.Println("TypeB.MethodA()")
}
...
var i SomeInterface
i = &TypeA{"A"}
b := &TypeB{"B"}
a, isTypeA := i.(*TypeA)
fmt.Printf("%v, %t\n", a, isTypeA) // will print {A}, true
b, isTypeB := i.(*TypeB)
fmt.Printf("%v, %t\n", b, isTypeB) // will print <nil>, false
Type Assertion
Type Switch
Type assertion with switch
:
var i SomeInterface
i = TypeA{"A"}
switch v := i.(type) {
case TypeA:
fmt.Printf("TypeA: %v\n", v)
case TypeB:
fmt.Printf("TypeB: %v\n", v)
}