Go does not allow truthiness
In TS, we can use a value as a boolean condition:
1const s = "hello";2const n = 3;3
4if (s) console.log("s is truthy");5if (n) console.log("n is truthy");6
7// output:8// s is truthy9// n is truthyBut in Go, we cannot.
1package main2
3import "fmt"4
5func main() {6 s := "hello"7 n := 38
9 // Illegal moves10 if s { // non-boolean condition in if statement11 fmt.Println("s truthy")12 }13 if n { // non-boolean condition in if statement14 fmt.Println("n truthy")15 }16
17 // Should be18 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.