DEV Community

Manigandan Dharmalingam
Manigandan Dharmalingam

Posted on

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
Info Comment hidden by post author - thread only accessible via permalink
Nicolas Lepage

Hi Manigandand

First of all welcome to dev.to and congrats for this first post.

I noticed the program of your post comes from a tweet from Dave Cheney:

I think it would be better if you cited this source.

I don't think dev.to has any rules against this kind of content reuse, and I'm not personnaly against it, however I find it more honest to always cite sources.

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