[Go] go-tour _ flowcontrol
ํด๋น๊ธ์ ์๋ ๋ด์ฉ์ ๋ฐํ์ผ๋ก ๊ณต๋ถํ ๋ด์ฉ๋ค์ ์ ๋ฆฌํ ๊ธ ์ ๋๋ค.
Go๋ฅผ ํฅํ ์ฌํ
go-tour-ko.appspot.com
for
package main
import "fmt"
func main() {
sum := 0
for i := 0; i < 10; i++ {
sum += i
}
fmt.Println(sum)
}
๊ดํธ๊ฐ ์๋ c๋๋
๋ฌธ๋ฒ์ด ์๋นํ c๋ ์ ์ฌํด์ ํฌ๊ฒ ๋ค๋ฅด์ง ์๊ฒ ์ฌ์ฉํ ์ ์๋๊ฑฐ๊ฐ๋ค.
For continued
package main
import "fmt"
func main() {
sum := 1
for ; sum < 1000; {
sum += sum
}
fmt.Println(sum)
}
for๋ฌธ์์ ์ด๊ธฐํ๊ตฌ๋ถ๊ณผ ์ฆ๊ฐ? ๊ตฌ๋ฌธ์ ํ์๊ฐ ์๋๋ค.
์์ ๊ฐ์ ํ์์ผ๋ก๋ ์ฌ์ฉ์ด ๊ฐ๋ฅํ๋ค.
For is Go's "while"
package main
import "fmt"
func main() {
sum := 1
for sum < 1000 {
sum += sum
}
fmt.Println(sum)
}
for๋ฌธ์์ ; ์ ์๋ตํ๋ ํ์์ผ๋ก ์ฌ์ฉํ๋ฉด
while๋ฌธ ์ฒ๋ผ ์ฌ์ฉ์ด ๊ฐ๋ฅํ๋ค.
๋ญ๊ฐ ๋๊ฒ ๋ฅ๋์ ์ด๋ผ๊ณ ์๊ฐ์ด ๋ค์๋ค.
์ธ๋ฐ์๋ ๊ฒ๋ค์ ๋์ด๋ธ ๋๋?
Forever
package main
func main() {
for {
}
}
for๋ฌธ์์ ์กฐ๊ฑด์ ์๊ฐํ๋ฉด ๋ฌดํ ๋ฃจํ์ ํ์์ผ๋ก ์ฌ์ฉ์ด ๊ฐ๋ฅํ๋ค.
If
package main
import (
"fmt"
"math"
)
func sqrt(x float64) string {
if x < 0 {
return sqrt(-x) + "i"
}
return fmt.Sprint(math.Sqrt(x))
}
func main() {
fmt.Println(sqrt(2), sqrt(-4))
}
if๋ฌธ์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ ํฌ๊ฒ c์ธ์ด์ ๋ค๋ฅด์ง์์๋ค.
๊ดํธ๊ฐ ์๋์ ๋..?
if x < 0
return sqrt(-x) + "i"
๋ค๋ง ์ด๋ฌํ ํ์์ผ๋ก ์ ๋๋ค๋๊ฐ? ์ ๋ฐ ๋ฐฉ๋ฒ์ ๋ถ๊ฐ๋ฅ {}์ด ํ์๋ก ์์ด์ผํ๋ค.
If with a short statement
package main
import (
"fmt"
"math"
)
func pow(x, n, lim float64) float64 {
if v := math.Pow(x, n); v < lim {
return v
}
return lim
}
func main() {
fmt.Println(
pow(3, 2, 10),
pow(3, 3, 20),
)
}
if๋ฌธ์ ์ฌ์ฉํ ๋ ์งง์ ๊ตฌ๋ฌธ์ ๋ฃ์ด์ ์ฌ์ฉ์ด ๊ฐ๋ฅํ๋ค.
์ฝ๋๋ฅผ ์กฐ๊ธ ๋ ์ค์ผ ์ ์๋๋ฐฉ๋ฒ?
- ํฐ ์๋ฅผ ๋น๊ตํ์ฌ ๋ฐํ
์์งํ ์ด๊ฑฐ๋ ์ ํ์ํ์ง ๋ชจ๋ฅด๊ฒ ๋ค..
์์ด๋ ๋ ๊ฑฐ๊ฐ๊ณ ์์ด๋ ๋ ๊ฑฐ๊ฐ์๋ฐ..
If and else
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),
)
}
if else ๋ฌธ์ ์ฌ์ฉ ์์ ๋ฅผ ๊ฐ๋จํ๊ฒ ํ์
if๋ฌธ์์ ์ฌ์ฉํ์๋ ์งง์ ๊ตฌ๋ฌธ์ else๋ฌธ์์๋ ์ฌ์ฉ์ด ๊ฐ๋ฅ
Exercise: Loops and Functions
package main
import (
"fmt"
"math"
)
func Sqrt(x float64) float64 {
z := 1.0
for i := 0; i < 10; i++ {
z -= (z*z - x) / (2 * z)
}
return z
}
func main() {
fmt.Println(Sqrt(2))
fmt.Println(math.Sqrt(2))
}
๋ฐ๋ณต๋ฌธ๊ณผ ํจ์๋ฅผ ์ค์ ๋ก ๋์ ํ์ฌ ์ฌ์ฉํด๋ด๋ผ๋ผ๋ ๋ฌธ์
์ ๊ณฑ๊ทผ ํจ์ ๊ตฌํํ๊ธฐ.
์ํ์ด ๋๋ฌด ์ด๋ ต๋ค..
Switch
package main
import (
"fmt"
"runtime"
)
func main() {
fmt.Print("Go runs on ")
switch os := runtime.GOOS; os {
case "darwin":
fmt.Println("OS X.")
case "linux":
fmt.Println("Linux.")
default:
// freebsd, openbsd,
// plan9, windows...
fmt.Printf("%s.\n", os)
}
}
switch ์ฌ์ฉ๋ฐฉ๋ฒ
os ์ ํ์ ์ด์์ฒด์ ์ ์ ๋ณด๋ฅผ ๋ด๊ณ switch๋ฅผ ํตํด์ ์ด์์ฒด์ ๊ฐ์ ๋น๊ตํ์ฌ ํ์ฌ ์ด์์ฒด์ ๋ฅผ ์ถ๋ ฅํ๋ค.
switch๋ฅผ ์์ฃผ ์ฌ์ฉํ์ง์์์ ๋ชฐ๋์๋๋ฐ
go์์๋ ๋ค๋ฅธ ์ธ์ด๋ค๊ณผ๋ ๋ค๋ฅด๊ฒ switch์ ์๋๊ฐ ๋น ๋ฅธํธ์ด๋ผ ์์ฃผ ์ฌ์ฉํ๋ค๊ณ ํ๋ค.
์ต์ํด์ง๋๊ฒ ์ข์๊ฑฐ๊ฐ๋ค.
Switch evaluation order
package main
import (
"fmt"
"time"
)
func main() {
fmt.Println("When's Saturday?")
today := time.Now().Weekday()
fmt.Println(today)
switch time.Saturday {
case today + 0:
fmt.Println("Today.")
case today + 1:
fmt.Println("Tomorrow.")
case today + 2:
fmt.Println("In two days.")
default:
fmt.Println("Too far away.")
}
}
switch๋ฌธ์ ๊ฒฝ์ฐ์๋ ์์์ ์๋๋ก ์งํ์ด ๋๋ค.
case์ ํด๋นํ๋ ๋ด์ฉ์ด ๋์ด ์์ ๋ค์ด๊ฐ๋ฉด ์๋ ๋ด์ฉ๋ค์ ์งํํ์ง์์
Switch with no condition
package main
import (
"fmt"
"time"
)
func main() {
t := time.Now()
fmt.Println(t.Hour())
switch {
case t.Hour() < 12:
fmt.Println("Good morning!")
case t.Hour() < 17:
fmt.Println("Good afternoon.")
default:
fmt.Println("Good evening.")
}
}
์๋ฌด ๊ฐ๋ ๋ฃ์ง์์ผ๋ฉด switch trueํ์์ผ๋ก ๋์ํ ์ ์๋ค๊ณ ํ๋ค.
๊ทธ๋ฅ switch๋ฌธ์ ์ฌ์ฉํ๋ ๊ฒ์ฒ๋ผ ์ฌ์ฉํ ์ ์๋ค๋ ์ด์ผ๊ธฐ์ธ ๊ฑฐ ๊ฐ๋ค.
Defer
package main
import "fmt"
func main() {
defer fmt.Println("world")
fmt.Println("hello")
}
defer๋ ํด๋น ํจ์๊ฐ ์ข ๋ฃ๋ ๋๊น์ง ํจ์์ ์คํ์ ์ฐ๊ธฐํ ์ ์๋ค.
์๋ฉธ์์ ๋น์ทํ ๋๋?
Stacking defers
package main
import "fmt"
func main() {
fmt.Println("counting")
for i := 0; i < 10; i++ {
defer fmt.Println(i)
}
fmt.Println("done")
}
defer ์์ฒด๋ ํ์ ์ ์ถ์ ์์น์ผ๋ก ํ๊ธฐ๋๋ฌธ์ ์คํ ํ์์ผ๋ก ์งํ์ด๋๋ค.
๋์ค์ ์ถ๋ ฅํ ๋ฌธ์๊ฐ ๋ง์ง๋ง์ ๋๊ฐ๋ ๋ฐฉ์