10 examples of 'golang type switch' in Go

Every line of 'golang type switch' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Go code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
159func singleCaseSwitch(x int) string {
160 switch x {
161 case 0:
162 return "0"
163 }
164 return fmt.Sprint(x)
165}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

355func TypeSwitch(init Stmt, x string, y Expr, body ...Stmt) *TypeSwitchStmt {
356 var assign Stmt
357 if x != "" {
358 assign = Init([]string{x}, AssertType(y))
359 } else {
360 assign = &ExprStmt{AssertType(y)}
361 }
362 return &TypeSwitchStmt{Init: init, Assign: assign, Body: *Block(body...)}
363}
2026func isTypeSwitchAssert(x ast.Expr) bool {
2027 a, ok := x.(*ast.TypeAssertExpr)
2028 return ok && a.Type == nil
2029}
17func fastpathDecodeTypeSwitch(iv interface{}, d *Decoder) bool { return false }
81func negativeTypeSwitchVarF5(x, y, z interface{}) int {
82 switch x := x.(type) {
83 case int:
84 switch y := y.(type) {
85 case int:
86 switch z := z.(type) {
87 case int:
88 return x + y + z
89 }
90 }
91 }
92 return 0
93}
145func (c *Comp) typeswitchNode(stmt ast.Stmt) (ast.Expr, string) {
146 var varname string // empty, or name of variable in 'switch varname := expression.(type)'
147 var tagnode ast.Expr
148 switch stmt := stmt.(type) {
149 case *ast.AssignStmt:
150 if len(stmt.Lhs) == 1 && len(stmt.Rhs) == 1 && stmt.Tok == token.DEFINE {
151 if lhs, ok := stmt.Lhs[0].(*ast.Ident); ok {
152 varname = lhs.Name
153 tagnode = stmt.Rhs[0]
154 }
155 }
156 case *ast.ExprStmt:
157 tagnode = stmt.X
158 }
159
160 for {
161 switch e := tagnode.(type) {
162 case *ast.ParenExpr:
163 tagnode = e.X
164 continue
165 case *ast.TypeAssertExpr:
166 if e.Type != nil {
167 c.Errorf("invalid type switch: expecting '.(type)', found type assertion: %v", stmt)
168 }
169 tagnode = e.X
170 default:
171 tagnode = e
172 }
173 break
174 }
175 if tagnode == nil {
176 c.Errorf("expected type-switch expression, found: %v", stmt)
177 }
178 return tagnode, varname
179}
27func (env *Env) evalTypeSwitch(node *ast.TypeSwitchStmt) (ret r.Value, rets []r.Value) {
28 // the scope of variables defined in the init and assign statements of a type switch
29 // is the type switch itself
30 if node.Init != nil {
31 env = NewEnv(env, "type switch")
32 env.evalStatement(node.Init)
33 }
34 varname, expr := env.mustBeTypeSwitchStatement(node.Assign)
35 v := env.evalExpr1(expr)
36 if node.Body == nil || len(node.Body.List) == 0 {
37 return NoneR, nil
38 }
39 var vt r.Type = nil
40 if v != NoneR && v != NilR {
41 // go through interface{} to obtain actual concrete type
42 val := v.Interface()
43 v = r.ValueOf(val)
44 if val != nil {
45 vt = v.Type()
46 }
47 }
48 var default_ *ast.CaseClause
49 for _, stmt := range node.Body.List {
50 case_ := stmt.(*ast.CaseClause)
51 if case_.List == nil {
52 // default will be executed later, if no case matches
53 default_ = case_
54 } else if t, ok := env.typecaseMatches(vt, case_.List); ok {
55 return env.evalTypecaseBody(varname, t, v, case_, false)
56 }
57 }
58 if default_ != nil {
59 return env.evalTypecaseBody(varname, TypeOfInterface, v, default_, true)
60 }
61 return NoneR, nil
62}
21func fastpathEncodeTypeSwitch(iv interface{}, e *Encoder) bool { return false }
120func OutputSwitch(key string, v interface{}) {
121 switch v.(type) {
122 case map[string]interface{}:
123 fmt.Println(key + "_type=map")
124 OutputMap(key+"_", v)
125 case []interface{}:
126 fmt.Println(key + "_type=array")
127 fmt.Println(key + "_length="+strconv.Itoa(len(v.([]interface{}))))
128 OutputArray(key+"_", v)
129 case string:
130 fmt.Println(key + "_type=string")
131 fmt.Print(key + "_value=")
132 str := v.(string)
133 str = strings.Replace(str, `'`, `'"'"'`, -1)
134 fmt.Printf("'%s'\n",str)
135 case int:
136 fmt.Println(key + "_type=int")
137 fmt.Println(key + "_value=" + strconv.Itoa(v.(int)))
138 case float64:
139 if v.(float64) == float64(int(v.(float64))) {
140 fmt.Println(key + "_type=int")
141 fmt.Println(key + "_value=" + strconv.Itoa(int(v.(float64))))
142 } else {
143 fmt.Println(key + "_type=float")
144 fmt.Println(key + "_value=" + strconv.FormatFloat(v.(float64), 'f', 4, 64))
145 }
146 case bool:
147 fmt.Println(key + "_type=bool")
148 fmt.Println(key + "_value=" + strconv.FormatBool(v.(bool)))
149 case nil:
150 fmt.Println(key + "_type=nil")
151 fmt.Println(key + "_value=")
152 default:
153 fmt.Println("I dont know how to deal with ", reflect.TypeOf(v))
154 os.Exit(1)
155 }
156}
12func newSwitch(v interface{}, cs []ast.SwitchCase, d interface{}) interface{} {
13 if len(cs) == 0 {
14 return d
15 }
16
17 return ast.NewSwitch(v, cs, d)
18}

Related snippets