-
Notifications
You must be signed in to change notification settings - Fork 0
/
conditions.go
71 lines (61 loc) · 1.26 KB
/
conditions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"fmt"
"strings"
"math"
"runtime"
)
// `if` statement do not needs to parentheses but reqires the braces
func GtLt(operation, text string, count int) bool {
if operation == "more" {
if strings.Count(text, "") > count {
return true
}
return false
} else if operation == "less" {
if strings.Count(text, "") < count {
return true
}
return false
}
return false
}
/*
* If with a short statement
* The `if` statement like `for` can start with a short statement to excute before the condition.
* The short declared statement can be used only in the `if` and `else` scopes
**/
func pow(x, y, limit float64) float64 {
if v:= math.Pow(x, y); v < limit {
return v
} else {
fmt.Printf("%g >= %g\n", v, limit)
}
return limit
}
/*
* Another type of condition statements is `switch`
*
**/
func getOS() string {
switch os := runtime.GOOS; os {
case "Linux":
return os;
case "Windows":
return os;
default:
return "Unknown"
}
}
/*
* Switch without a condition is equal to `switch true`
* switch {
* // some code
* }
**/
func main() {
var more = GtLt("more" , "Go Programming Language !", 10)
var less = GtLt("less", "Go Programmin Language !", 10)
fmt.Println("result =>", more)
fmt.Println("result =>", less)
}