Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

10〜30分で何となく分かるGo

6,888 views

Published on

Published in: Technology, Business
  • Be the first to comment

10〜30分で何となく分かるGo

  1. 1. #moriyoshi
  2. 2. $ export GOROOT=$HOME/go $ export GOBIN=$HOME/go/bin $ export GOOS=linux darwin $ export GOARCH=amd64 x86 $ export PATH=$GOBIN:$PATH $ mkdir -p $GOBIN $ hg clone -r release https://go.googlecode.com/hg/ $GOROOT $ cd $GOROOT/src $ ./all.bash
  3. 3. package main import "fmt" func main() { fmt.Printf(" !"); }
  4. 4. $ export GOROOT=$HOME/go $ export GOBIN=$HOME/go/bin $ export GOOS=linux $ export GOARCH=amd64 $ export PATH=$GOBIN:$PATH $ 6g test.go $ 6l -o test test.6 $ ./test
  5. 5. ?
  6. 6. package main import "fmt" func main() { str := " "; str += " "; fmt.Printf("%sn", str); }
  7. 7. package main import "fmt" func main() { arr := [4]int { 1, 2, 3, 4}; fmt.Printf("0: %dn", arr[0]); fmt.Printf("1: %dn", arr[1]); fmt.Printf("2: %dn", arr[2]); fmt.Printf("3: %dn", arr[3]); }
  8. 8. package main import "fmt" func main() { str1 := "Hello, world!"; str2 := " , "; fmt.Printf("%s (%d)n", str1, len(str1)); fmt.Printf("%sn", str1[0:5]); fmt.Printf("%s (%d)n", str2, len(str2)); fmt.Printf("%sn", str2[0:5]); // WTF! }
  9. 9. package main import "fmt" import "utf8" func main() { str1 := "Hello, world!"; str2 := " , "; fmt.Printf("%s (%d)n", str1, len(str1)); fmt.Printf("%sn", str1[0:5]); fmt.Printf("%s (%d)n", str2, utf8.RuneCountInString(str2)); // FIXME: utf8 substring }
  10. 10. package main import "fmt" func main() { str := "Hello"; for i, c := range str { fmt.Printf("%d: %cn", i, c); } }
  11. 11. package main import "fmt" func main() { str := "Hello"; for i := 0; i < len(str); i += 1 { fmt.Printf("> %cn", str[i]); } }
  12. 12. package main import "fmt" func main() { str := "Hello"; i := 0; for { if i >= len(str) { break } fmt.Printf("> %cn", str[i]); i += 1; } }
  13. 13. package main import "bufio" import "os" import "fmt" func main() { in := bufio.NewReader(os.Stdin); for { str, e := in.ReadString('n'); if e != nil { if e != os.EOF { fmt.Printf("Error: %sn", e); } break } os.Stdout.WriteString("-> "); os.Stdout.WriteString(str); } }
  14. 14. package main import "fmt" import "regexp" func main() { re, e := regexp.Compile("[A-Z][a-z]+"); if e != nil { fmt.Printf("Error: %sn", e); return } fmt.Printf("%tn", re.MatchString("test")); matches := re.MatchStrings("Abc Def "); for i := range matches { fmt.Printf("%sn", matches[i]); } str := "test Test abc"; fmt.Printf("%sn", re.ReplaceAllString(str , "***")); }
  15. 15. package main import ( "io"; "fmt"; "http" ) func main() { url := "http://search.twitter.com/search.json?q=" + http.URLEscape("#moriyoshi"); resp, _, e := http.Get(url); if e != nil { goto Error; } body, e := io.ReadAll(resp.Body); if e != nil { goto Error; } resp.Body.Close(); fmt.Printf(">>> %sn", string(body)); return; Error: fmt.Printf("Error: %sn", e); return; }
  16. 16. package main import ( "io"; "fmt"; "http"; "json"; ) func jsonGet(url string) json.Json { resp, _, e := http.Get(url); if e != nil { goto Error; } body, e := io.ReadAll(resp.Body); if e != nil { goto Error; } resp.Body.Close(); jsonObj, ok, etok := json.StringToJson(string(body)); if !ok { fmt.Printf("JSON syntax error near: " + etok); return nil; } return jsonObj; Error: fmt.Printf("Error: %sn", e); return nil; }
  17. 17. func main() { url := "http://search.twitter.com/search.json?q=" + http.URLEscape("#moriyoshi"); j := jsonGet(url); results := json.Walk(j, "results"); for i := 0; i < results.Len(); i += 1 { r := results.Elem(i); fmt.Printf("%s: %sn", r.Get("from_user"), r.Get("text")); } return; }
  18. 18. package main import ( "fmt"; "time"; ) func sub(n int) { fmt.Printf("Goroutine #%d started.n", n); for i := 0; i < 5; i += 1 { fmt.Printf("%d: %dn", n, i); time.Sleep(1e9); } } func main() { for i := 0; i < 10; i += 1 { go sub(i); } time.Sleep(5e9); }
  19. 19. package main import ( "fmt"; "time"; ) func sub(n int, ch chan int) { fmt.Printf("Goroutine #%d started.n", n); for i := 0; i < 5; i += 1 { fmt.Printf("%d: %dn", n, i); time.Sleep(1e8); } ch <- n }
  20. 20. func main() { ch := make(chan int); for n := 0; n < 10; n += 1 { go sub(n, ch); time.Sleep(4e8) } for i := 0; i < 10; i += 1 { n := <- ch; fmt.Printf("Goroutine #%d ended. n", n) } }
  21. 21. package main import ( "fmt"; ) type Employee struct { name string; } type Company struct { name string; employees []Employee; } func main() { c := Company { name:"Acme" }; c.employees = []Employee { Employee { name:"Foo" }, Employee { name:"Bar" } }; fmt.Printf("Company name: %sn", c.name); fmt.Printf("Employees: %dn", len(c.employees)); for _, employee := range c.employees { fmt.Printf(" %sn", employee.name); } }
  22. 22. package main import ( "fmt"; ) func main() { m := make(map[string] string); m["Foo"] = "Hoge"; m["Bar"] = "Fuga"; for k, v := range m { fmt.Printf("%s: %sn", k, v) } }
  23. 23. package main import ( "fmt"; ) type MyInt int; type Foo struct { values []int; } func (self *Foo) sum() int { retval := 0; for _, v := range self.values { retval += v } return retval } func main() { f := &Foo { values: []int { 0, 1, 2, 3, 4 } }; fmt.Printf("%dn", f.sum()); }
  24. 24. package main import ( "fmt"; ) type MyInt int; func (self MyInt) add(rhs int) MyInt { return MyInt(int(self) + rhs) } func main() { fmt.Printf("%dn", MyInt(1).add(4).add(5)); }
  25. 25. package main import ( "fmt" ) type IHello interface { sayHello() } type IKonnichiwa interface { sayKonnichiwa() } type IBilingual interface { IHello; IKonnichiwa }
  26. 26. type TypeA int; type TypeB int; type TypeC int; func (_ TypeA) sayHello() {} func (_ TypeB) sayKonnichiwa() {} func (_ TypeC) sayHello() {} func (_ TypeC) sayKonnichiwa() {} func hello(_ IHello) { fmt.Printf("Hellon") } func konnichiwa(_ IKonnichiwa) { fmt.Printf("Konnichiwan") }
  27. 27. func main() { var ( a TypeA; b TypeB; c TypeC ); hello(a); konnichiwa(b); hello(c); konnichiwa(c); }
  28. 28. package main type Foo struct { a int } type Bar struct { b int } type FuBar struct { *Foo; *Bar }
  29. 29. func (_ *Foo) doSomething() {} func (_ *Bar) doIt() {} func main() { fubar := new(FuBar); fubar.doSomething(); fubar.doIt() }
  30. 30. package main import "fmt"; func main() { fmt.Printf("%dn", func(f func(func(int) int) int) int { return f(func(a int) int { return a + 2 }) }(func(g func(int) int) int { return g(1) })); }
  31. 31. package main import "fmt" type UntypedFunc func(Any) Any; type Any struct { UntypedFunc; int } func main() { // Y-combinator result := func(f Any) Any { return func(x Any) Any { return f.UntypedFunc( Any{ UntypedFunc: func(y Any) Any { return x.UntypedFunc(x).UntypedFunc(y) } } ) }( Any{ UntypedFunc: func(x Any) Any { return f.UntypedFunc( Any{ UntypedFunc: func(y Any) Any { return x.UntypedFunc(x).UntypedFunc(y) } } ) } } ); }(Any{ UntypedFunc: func(f Any) Any { return Any{ UntypedFunc: func(n Any) Any { if (n.int == 0) { return Any{ int: 1 } } return Any{ int: f.UntypedFunc(Any{ int: n.int - 1 }).int * n.int } } } } } ); fmt.Printf("%dn", result.UntypedFunc(Any{ int: 5 }).int); }
  32. 32. package main import "fmt"; func sliceInfo(arr []int) { fmt.Printf("len(arr)=%dn", len(arr)); fmt.Printf("cap(arr)=%dn", cap(arr)); } func arrayInfo(arr *[4]int) { fmt.Printf("len(arr)=%dn", len(arr)); fmt.Printf("cap(arr)=%dn", cap(arr)); } func main() { arr := [4]int { 1, 2, 3, 4 }; arrayInfo(&arr); // doesn't compile // sliceInfo(arr); sliceInfo(&arr); // doesn't compile // arrayInfo(arr[0:2]); sliceInfo(arr[0:2]) }

×