Top Banner
You can read Go code 9 March 2013 Homin Lee GDG Korea Golang
31

You can read go code

Jul 08, 2015

Download

Documents

Homin Lee
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: You can read go code

You can read Go code9 March 2013

Homin LeeGDG Korea Golang

Page 2: You can read go code

About me

주로 C/C++을 쓰며 Python을 서브로 사용해 왔습니다.

본격적으로 고 언어를 쓰기 시작한 지는 6개월 쯤 되었습니다.

Page 3: You can read go code

Goal

고 언어의 존재를 알림

고 언어로 작성한 코드를 읽을 수 있게 함

매력 포인트 발산

고로 만든 프로젝트 자랑

세계정복

Page 4: You can read go code

Hello World

Page 5: You can read go code

Hello Pororo

C 계열 언어들과 닮은 친근한 문법

package main

import "fmt"

func main() { fmt.Println("뽀로로야 안녕")}

Page 6: You can read go code

Hello Friends

func printHelloTo(name string) { fmt.Printf("%s야 안녕\n", name)}

func main() { friends := []string{ "뽀로로", "크롱", "에디", "포비", "해리", "패티", "루피", }

for _, f := range friends { printHelloTo(f) }}

낚인건가? 왜죠?

Page 7: You can read go code

golang.org

Page 8: You can read go code

Go's declarations read left to right

Page 9: You can read go code

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 타입.

Page 10: You can read go code

Assign in gopher style

다음과 같이 선언과 할당을 풀어 쓸 수도 있지만,

var ok boolok = true

간편하게 := 를 사용해 다음과 같이 줄일 수 있습니다.

ok := true

아래에서 변수, numbers의 타입은;

numbers := []uint64{1, 2, 3, 5}

[]uint64가 됩니다.

Page 11: You can read go code

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 타입을 반환함.

Page 12: You can read go code

For with range

배열, 슬라이스, 스트링등은 range로 편하게 뽑아 쓸 수 있습니다.

package main

import "fmt"

func main() { for i, n := range []uint64{1, 2, 3, 5} { fmt.Println(i, n) }}

이제 고 코드를 읽을 준비가 다 되었습니다.

Page 13: You can read go code

Working?

package main

import ( "fmt")

func printHelloTo(name string) { fmt.Printf("%s야 안녕\n", name)}

func main() { friends := []string{ "뽀로로", "크롱", "에디", "포비", "해리", "패티", "루피", }

for _, f := range friends { printHelloTo(f) }}

Page 14: You can read go code

Exercise

Page 15: You can read go code

Exercise: Struct

구조체(struct)역시 왼쪽에서 오른쪽으로 읽힙니다.

type Circle struct { X, Y int // center R uint // radius}

읽어보세요 :)

Page 16: You can read go code

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)}

읽어보세요 :)

Page 17: You can read go code

Third-party packages

Page 18: You can read go code

Godog.org

공식 문서 사이트에서는 유명 소스 호스팅 사이트 들을 크롤링해 써드파티 패키지들 까지 웹 문서를 제공합니다.

Page 19: You can read go code

Download third-party package

import ( "fmt" "github.com/suapapa/go_hangul")

써드파티 패키지가 포함된 프로그램은 빌드전에 패키지들을 다운로드 받아야 합니다.

$ go get

패키지는 다음 위치에 다운 받아 집니다

$GOPATH/src/... : Workspace 경로 밑

$GOROOT/src/pkg/... : 고 설치 위치 밑 내장 패키지들이 있는 그 곳

Page 20: You can read go code

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))}

Page 21: You can read go code

Is it fast?

Page 22: You can read go code

Go vs ???

Computer Language Benchmarks Game (http://benchmarksgame.alioth.debian.org/u64/benchmark.php?test=all&lang=go&lang2=java)

Page 23: You can read go code

Go routine and channel

Page 24: You can read go code

Live demo

완성된 게임은 아래 Github에 있습니다.

github.com/suapapa/whac-a-gopher (https://github.com/suapapa/whac-a-gopher)

Page 25: You can read go code

IMO

Page 26: You can read go code

Googling with "golang"

Page 27: You can read go code

golang.org

Go is an open source programming environment that makes it easy to build simple, reliable, and efficient software.

Page 28: You can read go code

A Tour of Go (한글)

go-tour-kr.appspot.com/ (http://go-tour-kr.appspot.com/)

Page 29: You can read go code

User group

GDG Korea Golang (한국 고 언어 사용자 모임) (https://plus.google.com/communities/115721275599816202991)

Page 30: You can read go code

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)

Page 31: You can read go code

Thank you

Homin LeeGDG Korea Golang@suapapa (http://twitter.com/suapapa)