์๋
ํ์ธ์. ๊ฐ๋ฐ์ ๋ชจ๋๋ฆฌ์
๋๋ค.
The Go Programming Language ๋ผ๋ ์ฑ
์ผ๋ก Go๋ฅผ ๊ณต๋ถํ๊ณ ์์ผ๋ฉฐ, ํด๋น ์ฑ
์ ๋ด์ฉ์ ์์ฝ ์ ๋ฆฌํด์ ์ฌ๋ฆฌ๋ ค๊ณ ํฉ๋๋ค. ์ ๋ ๋ฒ์ญ๋ณธ์ ๊ตฌ๋งคํด์ ๊ณต๋ถํ๊ณ ์์ต๋๋ค.
๊ฒ์๋ฌผ์ ์์ ์ฝ๋ ๋ผ๊ณ ๋์ค๋ ๊ฒ๋ค์ https://github.com/modolee/tgpl.git ์์ ๋ค์ด ๋ฐ์ผ์ค ์ ์์ต๋๋ค.
์์
tgpl/ch1/helloworldํจํค์ง ํ์ผ๋ค์$GOPATH/src/tgpl/ch1/hellworld๋๋ ํ ๋ฆฌ์ ์ ์ฅ๋ฉ๋๋ค.
2.5์์ ์์ฑํ๋ ํจํค์ง๋ฅผ ๋ณํํด์ tgpl/ch2/tempconv๋ผ๋ ํจํค์ง๋ฅผ ๋ง๋ค์ด ๋ด ๋๋ค.
ํจํค์ง์์ ๊ฐ๋ณ ํ์ผ์ ์ ์ธ์ ์ ๊ทผํ๋ ๋ฐฉ๋ฒ์ ๋ณด์ฌ์ฃผ๊ธฐ ์ํด ํจํค์ง ์์ฒด๋ ๋ ๊ฐ์ ํ์ผ์ ์ ์ฅ๋ผ ์์ต๋๋ค.
์ค์ ๋ก๋ ์ด๋ฐ ์์ฑ ํจํค์ง๋ ํ์ผ ํ๋๋ฉด ์ถฉ๋ถํฉ๋๋ค.
ํ์ , ์์, ๋ฉ์๋ ์ ์ธ๋ค์ tempconv.go์ ๋ฃ์ต๋๋ค.
//tempconv ํจํค์ง๋ ์ญ์จ์ ํ์จ ๋ณํ๋ฅผ ์ํํฉ๋๋ค.
package tempconv
import "fmt"
type Celsius float64
type Fahrenheit float64
const (
AbsoluteZeroC Celsius = -273.15
FreezingC Celsius = 0
BoilingC Celsius = 100
)
func (c Celsius) String() string { return fmt.Sprintf("%gยบC", c) }
func (f Fahrenheit) String() string { return fmt.Sprintf("%gยบF", c) }
์์ ์ฝ๋ [ch2/tempconv/tempconv.go]
package tempconv
// CToF๋ ์ญ์จ์จ๋๋ฅผ ํ์จ์จ๋๋ก ๋ณํํฉ๋๋ค.
func CToF(c Celsius) Fahrenheit { return Fahrenheit(c*9/5 + 32) }
// FToC๋ ํ์จ์จ๋๋ฅผ ์ญ์จ์จ๋๋ก ๋ณํํฉ๋๋ค.
func FToC(f Fahrenheit) Celsius { return Celsius((f - 32) * 5 / 9) }
์์ ์ฝ๋ [ch2/tempconv/conv.go]
fmt.Printf("Brrrr! %v\n", tempconv.AbsoulteZeroC) // "Brrrr! -273.15ยบC"//Cf๋ ์ซ์ ์ธ์๋ฅผ ์ญ์จ์ ํ์จ๋ก ๋ณํํฉ๋๋ค.
package main
import (
"fmt"
"os"
"strconv"
"tgpl/ch2/tempconv"
)
func main() {
for _, arg := range os.Args[1:] {
t, err := strconv.ParseFloat(arg, 64)
if err != nil {
fmt.Fprintf(os.Stderr, "cf: %v\n", err)
os.Exit(1)
}
f := tempconv.Fahrenheit(t)
c := tempconv.Celsius(t)
fmt.Printf("%s = %s, %s = %s\n", f, tempconv.FToC(f), c, tempconv.CToF(c))
}
}
์์ ์ฝ๋ [tgpl/ch2/cf/main.go]
์คํ๊ฒฐ๊ณผ
$ go build tgpl/ch2/cf
$ ./cf 32
32ยบF = 0ยบC, 32ยบC = 89.6ยบF
$ ./cf 212
212ยบF = 100ยบC, 212ยบC = 413.6ยบF
$ ./cf -40
-40ยบF = -40ยบC, -40ยบC = -40ยบF
var a = b + c // a๊ฐ 3์ผ๋ก ์ธ ๋ฒ์งธ๋ก ์ด๊ธฐํ ๋จ
var b = f() // b๋ f๋ฅผ ํธ์ถํจ์ผ๋ก์จ ๋ ๋ฒ์งธ๋ก ์ด๊ธฐํ ๋จ
var c = 1 // c๊ฐ ๋จผ์ 1๋ก ์ด๊ธฐํ ๋จ
func f() int { return c + 1 }
func init() { /* ... */ }q๋ฅผ ์ํฌํธํ๋ p ํจํค์ง์์๋ p์ ์ด๊ธฐํ ๊ณผ์ ์ด ์์๋๊ธฐ ์ ์ q์ ์ด๊ธฐํ๊ฐ ์๋ฃ๋๋ค๋ ๊ฒ์ ํ์ ํ ์ ์์ต๋๋ค.package popcount
// pc[i]์ i์ ์ธ๊ตฌ์์
๋๋ค.
var pc [256]byte
func init() {
for i := range pc {
pc[i] = pc[i/2] + byte(i&1)
}
}
// PopCount๋ x์ ์ธ๊ตฌ์(1๋ก ์ง์ ๋ ๋นํธ ์)๋ฅผ ๋ฐํํฉ๋๋ค.
func PopCount(x uint64) int {
return int(pc[byte(x>>(0*8))] +
pc[byte(x>>(1*8))] +
pc[byte(x>>(2*8))] +
pc[byte(x>>(3*8))] +
pc[byte(x>>(4*8))] +
pc[byte(x>>(5*8))] +
pc[byte(x>>(6*8))] +
pc[byte(x>>(7*8))])
}
์์ ์ฝ๋ [tgpl/ch2/popcount/popcount.go]
package main
import (
"fmt"
"os"
"strconv"
"tgpl/ch2/popcount"
)
func main() {
for _, arg := range os.Args[1:] {
pc, err := strconv.ParseUint(arg, 10, 10)
if err != nil {
fmt.Fprintf(os.Stderr, "popcount: %v\n", err)
os.Exit(1)
}
fmt.Println(popcount.PopCount(pc))
}
}
์์ ์ฝ๋ [tgpl/ch2/popcount_main.go]
์คํ๊ฒฐ๊ณผ
$ go run ch2/popcount_main.go 255
8
[The Go Programming Language] 2์ฅ ํ๋ก๊ทธ๋จ ๊ตฌ์กฐ - 2.7 ๋ฒ์