Booleans

Go doesn't have a quick way to evaluate if something is"truthy".In Python, for example, you can use an if statement on any type andmost types have a way of automatically converting to True orFalse. For example you can do:

  1. x = 1
  2. if x:
  3. print "Yes"
  4. y = []
  5. if y:
  6. print "this won't be printed"

This is not possible in Go. You really need to do it explicitly forevery type:

  1. x := 1
  2. if x != 0 {
  3. fmt.Println("Yes")
  4. }
  5. var y []string
  6. if len(y) != 0 {
  7. fmt.Println("this won't be printed")
  8. }

Python

  1. print True and False # False
  2. print True or False # True
  3. print not True # False

Go

  1. package main
  2.  
  3. import "fmt"
  4.  
  5. func main() {
  6. fmt.Println(true && false) // false
  7. fmt.Println(true || false) // true
  8. fmt.Println(!true) // false
  9.  
  10. x := 1
  11. if x != 0 {
  12. fmt.Println("Yes")
  13. }
  14. var y []string
  15. if len(y) != 0 {
  16. fmt.Println("this won't be printed")
  17. }
  18.  
  19. }