10 examples of 'golang switch case' in Go

Every line of 'golang switch case' 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
159func singleCaseSwitch(x int) string {
160 switch x {
161 case 0:
162 return "0"
163 }
164 return fmt.Sprint(x)
165}
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}
107func (v *BaseECMAScriptVisitor) VisitCaseBlock(ctx *CaseBlockContext) interface{} {
108 return v.VisitChildren(ctx)
109}
772func (s *Statement) Case(cases ...Code) *Statement {
773 g := &Group{
774 close: ":",
775 items: cases,
776 multi: false,
777 name: "case",
778 open: "case ",
779 separator: ",",
780 }
781 *s = append(*s, g)
782 return s
783}
16func NewSwitch(v interface{}, cs []SwitchCase, d interface{}) Switch {
17 if len(cs) == 0 && d == nil {
18 panic("Cases in a match expression must be more than 0.")
19 }
20
21 return Switch{v, cs, d}
22}
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}
179func switchTrue(x int) string {
180 switch true {
181 case x > 10:
182 return "more than 10"
183 default:
184 return "?"
185 }
186}
430func (S *Scanner) switch4(tok0, tok1 token.Type, ch2 rune, tok2, tok3 token.Type) token.Type {
431 if S.ch == '=' {
432 S.next()
433 return tok1
434 }
435 if S.ch == ch2 {
436 S.next()
437 if S.ch == '=' {
438 S.next()
439 return tok3
440 }
441 return tok2
442 }
443 return tok0
444}
1010func (r *importReader) caseList(switchExpr ir.Node) []*ir.CaseClause {
1011 namedTypeSwitch := isNamedTypeSwitch(switchExpr)
1012
1013 cases := make([]*ir.CaseClause, r.uint64())
1014 for i := range cases {
1015 cas := ir.NewCaseStmt(r.pos(), nil, nil)
1016 cas.List = r.stmtList()
1017 if namedTypeSwitch {
1018 cas.Var = r.localName()
1019 cas.Var.Defn = switchExpr
1020 }
1021 cas.Body = r.stmtList()
1022 cases[i] = cas
1023 }
1024 return cases
1025}
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}

Related snippets