Go Type Switch: Difference between revisions
Jump to navigation
Jump to search
(Created page with "=Internal= =Overview= A type switch is a new control structure introduced by Go. Type assertion with <code>switch</code>: <syntaxhighlight lang='go'> 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) } </syntaxhighlight>") |
|||
Line 1: | Line 1: | ||
=Internal= | =Internal= | ||
* [[Go_Type_Assertions#Overview|Type Assertion]] | |||
=Overview= | =Overview= | ||
Revision as of 23:27, 13 August 2024
Internal
Overview
A type switch is a new control structure introduced by Go.
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)
}