Skip to content
loading...

Dave Cheney #golang pop quiz

twitter logo github logo ・1 min read  

#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))
}
twitter logo DISCUSS (9)
markdown guide
 

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))
}

 
 
 
 
 
Classic DEV Post from Mar 7

Let's talk about Linux

a tiny comic about Linux

Manigandan Dharmalingam profile image
Gopher | Software Engineer