Go does not allow truthiness

In TS, we can use a value as a boolean condition:

1
const s = "hello";
2
const n = 3;
3
4
if (s) console.log("s is truthy");
5
if (n) console.log("n is truthy");
6
7
// output:
8
// s is truthy
9
// n is truthy

But in Go, we cannot.

1
package main
2
3
import "fmt"
4
5
func main() {
6
s := "hello"
7
n := 3
8
9
// Illegal moves
10
if s { // non-boolean condition in if statement
11
fmt.Println("s truthy")
12
}
13
if n { // non-boolean condition in if statement
14
fmt.Println("n truthy")
15
}
16
17
// Should be
18
if s != "" {
19
fmt.Println("s is non-empty")
20
}
21
if n != 0 {
22
fmt.Println("n is non-zero")
23
}
24
}

Wow! You read the whole thing. People who make it this far sometimes want to receive emails when I post something new.

I also have an RSS feed.