10 examples of 'golang switch fallthrough' in Go

Every line of 'golang switch fallthrough' 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

179func switchTrue(x int) string {
180 switch true {
181 case x > 10:
182 return "more than 10"
183 default:
184 return "?"
185 }
186}
109func (s *Scanner) switch2(tok0, tok1 Token) Token {
110 if s.ch == '=' {
111 s.next()
112 return tok1
113 }
114 return tok0
115}
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}
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}
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}
131func switchTrue(m dsl.Matcher) {
132 m.Match(`switch true { $*_ }`).
133 Report(`replace 'switch true {}' with 'switch {}'`)
134 m.Match(`switch $x; true { $*_ }`).
135 Report(`replace 'switch $x; true {}' with 'switch $x; {}'`)
136}
457func (c *simplifyContext) simplifySwitch(stmts *[]ast.Stmt, s *ast.SwitchStmt) {
458 wrapClause := &ast.CaseClause{}
459 newS := &ast.SwitchStmt{
460 Switch: s.Switch,
461 Body: &ast.BlockStmt{List: []ast.Stmt{wrapClause}},
462 }
463 c.info.Scopes[newS] = c.info.Scopes[s]
464 c.info.Scopes[wrapClause] = c.info.Scopes[s]
465 *stmts = append(*stmts, newS)
466 stmts = &wrapClause.Body
467
468 c.simplifyStmt(stmts, s.Init)
469
470 nonDefaultClauses, defaultClause := c.simplifyCaseClauses(s.Body.List)
471 tag := c.makeTag(stmts, s.Tag, len(nonDefaultClauses) != 0)
472 *stmts = append(*stmts, unwrapBlock(c.switchToIfElse(tag, nonDefaultClauses, defaultClause))...)
473}
39func noWarningsNonEmptyFallthroughInNestedSwitch(i, j int) bool {
40 switch i {
41 case 0:
42 return true
43 case 1:
44 switch j {
45 case 0:
46 fmt.Print("")
47 fallthrough
48 case 1:
49 return true
50 default:
51 return false
52 }
53 case 2:
54 fmt.Print("")
55 fallthrough
56 default:
57 return false
58 }
59}
2053func (e *encoder) astSwitch(n *ast.Switch) (outID uint64) {
2054 e.build(&e.instances.AstSwitch, e.maps.ASTSwitch, n, &outID, func() *ASTSwitch {
2055 p := &ASTSwitch{
2056 Value: e.astNode(n.Value),
2057 Default: e.astDefault(n.Default),
2058 }
2059 foreach(n.Cases, e.astCase, &p.Cases)
2060 return p
2061 })
2062 return
2063}

Related snippets