10 examples of 'golang if else' in Go

Every line of 'golang if else' 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
391func (v *BaseFeelParserVisitor) VisitOrElse(ctx *OrElseContext) interface{} {
392 return v.VisitChildren(ctx)
393}
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

1822func (s *IfContext) ELSE() antlr.TerminalNode {
1823 return s.GetToken(COOLParserELSE, 0)
1824}
422func (p *parser) elseControl() Node {
423 return newElse(p.expect(itemRightDelim, "else").pos, p.lex.lineNumber())
424}
172func else_(ctx *EngineContext, scope *Scope, ins protoface.Instruction, args []interface{}) (returnValue interface{}) {
173 returnValue = "false"
174 if ctx.ShouldContinue() {
175 ctx.SetShouldContinue(false)
176 for i := 0; i < ins.INumChildren(); i++ {
177 child := ins.IGetNthChild(i)
178 ctx.RunInstruction(scope, child)
179 }
180 returnValue = "true"
181 }
182 return
183}
120func (p *parser) if_() Statement {
121 pos := p.pos
122 p.expect(IF)
123 condition := p.expression()
124 body := p.block()
125 var elseBody Block
126 if p.tok == ELSE {
127 p.next()
128 if p.tok == LBRACE {
129 elseBody = p.block()
130 } else if p.tok == IF {
131 elseBody = Block{p.if_()}
132 } else {
133 p.error("expected { or if after else, not %s", p.tok)
134 }
135 }
136 return &If{pos, condition, body, elseBody}
137}
385func (e *emitter) If(i *parse.If) {
386 e.Expr(i.Cond)
387 e.asm("test %%rax, %%rax\n")
388 e.asm("jz %s\n", i.LElse)
389 e.Stmt(i.Stmt)
390 e.raw("%s:\n", i.LElse)
391 if i.Else != nil {
392 e.Stmt(i.Else)
393 }
394}
965func (t *Template) elseControl() Node {
966 // Special case for "else if".
967 peek := t.peekNonSpace()
968 if peek.typ == itemIf {
969 // We see "{{else if ... " but in effect rewrite it to {{else}}{{if ... ".
970 return t.newElse(peek.pos, t.lex.lineNumber())
971 }
972 return t.newElse(t.expectRightDelim("else").pos, t.lex.lineNumber())
973}
47func (p *Interpreter) If(e interpreter.Engine) {
48
49 var elseCode interface{}
50
51 stk := p.stk
52 elseArity := stk.popArity()
53 if elseArity == 1 {
54 elseCode, _ = stk.Pop()
55 }
56
57 condArity := stk.popArity()
58 condArity++
59
60 ifbr := stk.popNArgs(condArity << 1) // 2 * (condArity + 1)
61 for i := 0; i < condArity; i++ {
62 condCode := ifbr[i<<1]
63 evalCode(e, p, "expr", condCode)
64 v, _ := stk.Pop()
65 cond, ok := v.(bool)
66 if !ok {
67 panic(ErrIfCondNotBoolean)
68 }
69 if cond {
70 bodyCode := ifbr[(i<<1)+1]
71 if bodyCode == nil {
72 stk.Push(nil)
73 } else {
74 evalCode(e, p, "doc", bodyCode)
75 }
76 return
77 }
78 }
79 if elseCode == nil {
80 stk.Push(nil)
81 } else {
82 evalCode(e, p, "doc", elseCode)
83 }
84}
243func (v *BaseFeelParserVisitor) VisitLoopElseStatement(ctx *LoopElseStatementContext) interface{} {
244 return v.VisitChildren(ctx)
245}
303func (c *Cmd) If(vars *Vars) interface{} {
304 v := c.Params[0].Eval(vars)
305 var ret interface{}
306 if v.(bool) {
307 ret = c.Params[1].Eval(vars)
308 } else if len(c.Params) > 2 && vars.Jump.Type == 0 {
309 ret = c.Params[2].Eval(vars)
310 }
311 return ret
312}

Related snippets