Skip to content

Instantly share code, notes, and snippets.

@sugilog sugilog/sample.go
Created Dec 28, 2014

Embed
What would you like to do?
条件分岐:if
package main
import (
"fmt"
"time"
)
func main() {
patternA()
patternB()
// patternC()
}
const THRESHOLD = 30
func patternA() {
i := time.Now().Second()
if i > THRESHOLD {
fmt.Println( "more than", THRESHOLD )
} else {
fmt.Println( "less than or equal to", THRESHOLD )
}
fmt.Println( "value:", i )
}
func patternB() {
threshold1 := THRESHOLD + 10
threshold2 := THRESHOLD - 10
if i := time.Now().Second(); i > threshold1 {
fmt.Println( "more than", threshold1 )
} else if i > threshold2 {
fmt.Println( "more than", threshold2 )
} else {
fmt.Println( "less", threshold1, threshold2 )
}
// fmt.Println( "value:", i )
}
// func patternC() {
// i := time.Now().Second()
// if i > THRESHOLD {
// message := fmt.Sprintf( "more than %d", THRESHOLD )
// } else {
// message := fmt.Sprintf( "less than or equal to %d", THRESHOLD )
// }
// fmt.Println( message )
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.