This is a creation in Article, where the information may have evolved or changed.
7. Toad Notes Go language--if,switch,defer
If
The IF statements do not appear to be the same as in C or Java, except that they do not have ' () ' (even forcing them to be used), and ' {} ' is required.
Package Main
Import (
"FMT"
"Math"
)
Func sqrt (x float64) string {
If x< 0 {
RETURNSQRT (-X) + "I"
}
Returnfmt. Sprint (Math. SQRT (x))
}
Func Main () {
Fmt. Println (sqrt (2), sqrt (-4))
}
Perform:
1.4142135623730951 2i
As with for, the ' if ' statement can execute a simple statement before the condition.
The scope of the variable defined by this statement is only within the IF range.
(Use V at the last return statement.) )
Package Main
Import (
"FMT"
"Math"
)
Func pow (x, N, Lim float64) float64 {
If V: =math. Pow (x, N); V < Lim {
Returnv
}
Returnlim
}
Func Main () {
Fmt. Println (
Pow (3,2, 10),
Pow (3,3, 20),
)
}
The results of the implementation are as follows:
920
Variables defined in the handy statement of if can also be used in any corresponding else block.
Package Main
Import (
"FMT"
"Math"
)
Func pow (x, N, Lim float64) float64 {
If V: =math. Pow (x, N); V < Lim {
Returnv
} else{
Fmt. Printf ("%g>=%g\n", V, Lim)
}
// You can't use it at the beginning. v the
Returnlim
}
Func Main () {
Fmt. Println (
Pow (3,2, 10),
Pow (3,3, 20),
)
}
Execution Result:
27>= 20
920
Switch
A struct (' struct ') is a collection of fields.
The branch terminates automatically unless the Fallthrough statement ends.
Perform:
Package Main
Import (
"FMT"
"Runtime"
)
Func Main () {
Fmt. Print ("Goruns on")
Switchos: = runtime. GOOS; OS {
Case "Darwin":
Fmt. Println ("OSX.")
Case "Linux":
Fmt. Println ("Linux.")
Default
FreeBSD, OpenBSD,
Plan9, Windows ...
Fmt. Printf ("%s.", OS)
}
}
Results:
Goruns on Windows.
The order in which switch is executed
The switch condition is executed from top to bottom and stops when the match succeeds.
For example
Switch I {
Case 0:
Case F ():
}
' F ' is not called when i==0. )
Code:
Package Main
Import (
"FMT"
"Time"
)
Func Main () {
Fmt. Println ("When ' Ssaturday?")
today:= time. Now (). Weekday ()
Switchtime. Saturday {
Casetoday + 0:
Fmt. Println ("Today.")
Casetoday + 1:
Fmt. Println ("Tomorrow.")
Casetoday + 2:
Fmt. Println ("Intwo days.")
Default
Fmt. Println ("Toofar away.")
}
}
Perform:
When ' Ssaturday?
Toofar away.
Switch with no conditions
A switch with no conditions is the same as ' switch true '.
This structure allows the long if-then-else chain to be written in a clearer form.
Package Main
Import (
"FMT"
"Time"
)
Func Main () {
T: =time. Now ()
switch{
Caset. Hour () < 12:
Fmt. Println ("goodmorning!")
Caset. Hour () < 17:
Fmt. Println ("Goodafternoon.")
Default
Fmt. Println ("goodevening.")
}
}
Perform:
Goodevening.
Defer
The defer statement delays the execution of the function until the upper function returns.
The parameters of the deferred call are generated immediately, but the function is not called before the upper function returns.
Code:
Package Main
Import "FMT"
Func Main () {
Deferfmt. PRINTLN ("World")
Fmt. Println ("Hello")
}
EXECUTE as follows:
Hello
World
Defer stack
The deferred function call is pressed into a stack. When the function returns, the deferred function call is called in the order of last in, first out.
Code:
Package Main
Import "FMT"
Func Main () {
Fmt. Println ("Counting")
For i:= 0; I < 10; i++ {
Deferfmt. Println (i)
}
Fmt. Println ("Done")
}
Perform:
Counting
Done
9
8
7
6
5
4
3
2
1
0