If

Go's if statements are like its for loops; the expression need not be surrounded by parentheses ( ) but the braces { } are required.

if.go

  1. package main
  2. import (
  3. "fmt"
  4. "math"
  5. )
  6. func sqrt(x float64) string {
  7. if x < 0 {
  8. return sqrt(-x) + "i"
  9. }
  10. return fmt.Sprint(math.Sqrt(x))
  11. }
  12. func main() {
  13. fmt.Println(sqrt(2), sqrt(-4))
  14. }