68 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			Markdown
		
	
	
	
	
	
			
		
		
	
	
			68 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			Markdown
		
	
	
	
	
	
| ---
 | |
| title: If in Go
 | |
| ---
 | |
| 
 | |
| # If in Go
 | |
| 
 | |
| Go's `if` statements are like its `for` loops; the expression need not be surrounded by parentheses
 | |
| `(` `)` but the braces `{` `}` are required.
 | |
| 
 | |
| ```go
 | |
| func sqrt(x float64) string {
 | |
| 	if x < 0 {
 | |
| 		return sqrt(-x) + "i"
 | |
| 	}
 | |
| 	return fmt.Sprint(math.Sqrt(x))
 | |
| }
 | |
| ```
 | |
| 
 | |
| Like `for`, the `if` statement can start with a short statement to execute before the condition.
 | |
| 
 | |
| Variables declared by the statement are only in scope until the end of the `if`.
 | |
| 
 | |
| ```go
 | |
| func pow(x, n, lim float64) float64 {
 | |
| 	if v := math.Pow(x, n); v < lim {
 | |
| 		return v
 | |
| 	}
 | |
| 	return lim
 | |
| }
 | |
| ```
 | |
| 
 | |
| ## If and else
 | |
| 
 | |
| Variables declared inside an `if` short statement are also available inside any of the `else` blocks.
 | |
| 
 | |
| ```go
 | |
| package main
 | |
| 
 | |
| import (
 | |
| 	"fmt"
 | |
| 	"math"
 | |
| )
 | |
| 
 | |
| func pow(x, n, lim float64) float64 {
 | |
| 	if v := math.Pow(x, n); v < lim {
 | |
| 		return v
 | |
| 	} else {
 | |
| 		fmt.Printf("%g >= %g\n", v, lim)
 | |
| 	}
 | |
| 	// can't use v here, though
 | |
| 	return lim
 | |
| }
 | |
| 
 | |
| func main() {
 | |
| 	fmt.Println(
 | |
| 		pow(3, 2, 10),
 | |
| 		pow(3, 3, 20),
 | |
| 	)
 | |
| }
 | |
| ```
 | |
| 
 | |
| Running the above program produces an output similar to the following output -
 | |
| ```    
 | |
| $ go run if.go
 | |
| 27 >= 20
 | |
| 9 20
 | |
| ```
 |