Upload
homin-lee
View
205
Download
2
Embed Size (px)
Citation preview
Hello Friends
func printHelloTo(name string) { fmt.Printf("%s야 안녕\n", name)}
func main() { friends := []string{ "뽀로로", "크롱", "에디", "포비", "해리", "패티", "루피", }
for _, f := range friends { printHelloTo(f) }}
낚인건가? 왜죠?
Variable
왼쪽에서 오른쪽으로 읽어 봅시다!
var x intvar y int
Variable x in int type. Variable y in int type.
변수 x 는 int 타입. 변수 y 는 int 타입.
같은 타입일때, 여러 줄의 변수 선언을 다음과 같이 한 줄로 줄일 수 있습니다.
var x, y int
Variable x and y in int type.
변수 x, y 는 int 타입.
Assign in gopher style
다음과 같이 선언과 할당을 풀어 쓸 수도 있지만,
var ok boolok = true
간편하게 := 를 사용해 다음과 같이 줄일 수 있습니다.
ok := true
아래에서 변수, numbers의 타입은;
numbers := []uint64{1, 2, 3, 5}
[]uint64가 됩니다.
Function
함수 역시 왼쪽에서 오른쪽으로 읽힙니다.
func printHelloTo(name string) { fmt.Printf("%s야 안녕\n", name)}
Function printHelloTo recevied name in string.
함수 printHelloTo 는 인자로, name을 string 타입으로 받음.
반환값이 함수 선언의 뒤에 오는 것도 같은 이유 입니다.
func Sum(a, b int) int
Function Sum recevied a and b in int and return int
함수 Sum은 인자로 a, b를 int 타입으로 받고, int 타입을 반환함.
For with range
배열, 슬라이스, 스트링등은 range로 편하게 뽑아 쓸 수 있습니다.
package main
import "fmt"
func main() { for i, n := range []uint64{1, 2, 3, 5} { fmt.Println(i, n) }}
이제 고 코드를 읽을 준비가 다 되었습니다.
Working?
package main
import ( "fmt")
func printHelloTo(name string) { fmt.Printf("%s야 안녕\n", name)}
func main() { friends := []string{ "뽀로로", "크롱", "에디", "포비", "해리", "패티", "루피", }
for _, f := range friends { printHelloTo(f) }}
Exercise: Struct
구조체(struct)역시 왼쪽에서 오른쪽으로 읽힙니다.
type Circle struct { X, Y int // center R uint // radius}
읽어보세요 :)
Exercise: Function
복잡한 함수 선언의 경우 왼쪽에서 오른쪽으로 읽히는 특징은 모호함을 줄여 줍니다.
func getCb() func(int, int) int { cbAdd := func(a, b int) int { return a + b } return cbAdd}
func calc(cb func(int, int) int, a, b int) int { return cb(a, b)}
func main() { sum := calc(getCb(), 3, 4) fmt.Println(sum)}
읽어보세요 :)
Download third-party package
import ( "fmt" "github.com/suapapa/go_hangul")
써드파티 패키지가 포함된 프로그램은 빌드전에 패키지들을 다운로드 받아야 합니다.
$ go get
패키지는 다음 위치에 다운 받아 집니다
$GOPATH/src/... : Workspace 경로 밑
$GOROOT/src/pkg/... : 고 설치 위치 밑 내장 패키지들이 있는 그 곳
Hello freind revisit
import ( "fmt" "github.com/suapapa/go_hangul")
func josa(o string) string { // Convert string(utf-8) to []rune(ucs-4) r := []rune(string(o))
// Check if last character has jongsung _, _, f := hangul.Split(r[len(r)-1]) if f == 0 { return "야" } return "아"}
func printHelloTo(name string) { fmt.Printf("%s%s 안녕\n", name, josa(name))}
Go vs ???
Computer Language Benchmarks Game (http://benchmarksgame.alioth.debian.org/u64/benchmark.php?test=all&lang=go&lang2=java)
Live demo
완성된 게임은 아래 Github에 있습니다.
github.com/suapapa/whac-a-gopher (https://github.com/suapapa/whac-a-gopher)
golang.org
Go is an open source programming environment that makes it easy to build simple, reliable, and efficient software.
A Tour of Go (한글)
go-tour-kr.appspot.com/ (http://go-tour-kr.appspot.com/)
User group
GDG Korea Golang (한국 고 언어 사용자 모임) (https://plus.google.com/communities/115721275599816202991)
References
Go's Declaration Syntax (http://golang.org/doc/articles/gos_declaration_syntax.html)
Effecgive Go (http://golang.org/doc/effective_go.html)
golang.org (http://golang.org)