Go Type Switch

From NovaOrdis Knowledge Base
Revision as of 23:26, 13 August 2024 by Ovidiu (talk | contribs) (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>")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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)
}