DEV Community

Manigandan Dharmalingam
Manigandan Dharmalingam

Posted on

4 2

Dave Cheney #golang pop quiz

#go

What is the output for this program?

Source: https://twitter.com/davecheney/status/1215006731610124288

package main

import (
    "fmt"
)

func isEven(v int) bool {
    if (v % 2) == 1 {
        goto false
    }
    return !false
false:
    return false

}

func main() {
    fmt.Println(isEven(2020))
}

Top comments (9)

Collapse
 
akshaybharambe14 profile image
Akshay Bharambe • Edited

Thanks, got to know that "true" and "false" can be a variable name or label. Here is another version.

package main

import (
    "fmt"
)

func isEven(v int) bool {
    false := !true

    if (v % 2) == 1 {
        goto true
    }
    return true
true:
    return false

}

func main() {
    fmt.Println(isEven(2020))
}

Collapse
 
manigandand profile image
Manigandan Dharmalingam

That's cool

Collapse
 
nlepage profile image
Nicolas Lepage
Comment hidden by post author
Collapse
 
manigandand profile image
Manigandan Dharmalingam

agree

Collapse
 
mkumards profile image
Madhu Kumar D S

true

Collapse
 
manigandand profile image
Manigandan Dharmalingam

yup! Good

Collapse
 
marcobustillo profile image
Marco B

true. because 2020 % 2 == 0

Collapse
 
cookie1599 profile image
Atrisa Endya N H

true

Collapse
 
sergey_telpuk profile image
Sergey Telpuk

Where is the tips1 there?

Some comments have been hidden by the post's author - find out more

AWS Q Developer image

Your AI Code Assistant

Generate and update README files, create data-flow diagrams, and keep your project fully documented. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay