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.

Go言語

766 views

Published on

Go言語とは?
社内LT大会(15分くらいで1人1言語紹介する催し)で発表した内容です。2014年7月頃の情報です。

Published in: Software
  • Be the first to comment

Go言語

  1. 1. GOゝㄒ @na_o_ys
  2. 2. Agenda • Goゝㄒ䛸䛿 • ≉ᚩ • ゝㄒ௙ᵝ • Interface • ୪ิฎ⌮䛾௙⤌䜏
  3. 3. Goゝㄒ䛸䛿 • Google䛜㛤Ⓨ䛧䛯䝁䞁䝟䜲䝷ゝㄒ • 2009ᖺ䚸Robert Griesemer, Rob Pike, Ken Thompson • ᑐ㇟䠖኱つᶍ䛺䝅䝇䝔䝮䝋䝣䝖䜴䜵䜰䚸䝃䞊䝞䝋䝣䝖䜴䜵䜰 • ప䝺䜲䝲䛸䛾ぶ࿴ᛶ䠄䝥䝻䝉䝇⟶⌮䚸䝅䝇䝔䝮䝁䞊䝹…䠅 • C/C++䛻୪䜆ᐇ⾜㏿ᗘ • 㛤Ⓨ⪅䛾ቑ኱䛻⪏䛘䜛䚸䝅䞁䝥䝹䛺Ὑ⦎䛥䜜䛯ゝㄒ䝉䝑䝖 C/C++䛻௦䜟䜛᪂䛧䛔㑅ᢥ⫥
  4. 4. Goゝㄒ䛾≉ᚩ • 㟼ⓗᆺ௜䛡㻌+ ᆺ᥎ㄽ • 䝛䜲䝔䜱䝤䝁䞊䝗䜈䛾䝁䞁䝟䜲䝹 • Linux, OS X, Free BSD, Windows • DuckTyping • 䜺䝧䞊䝆䝁䝺䜽䝅䝵䞁 • ㇏ᐩ䛺䝷䜲䝤䝷䝸 Cゝㄒ䛻ືⓗᆺ௜䛡ゝㄒ䛾ᢏ⾡䜢ྲྀ䜚㎸䜣䛰
  5. 5. ゝㄒ௙ᵝ • ᇶᮏ䛿C䛾ᣑᙇ • 㛵ᩘ䛸ᵓ㐀య(+䝯䝋䝑䝗) • ᇶᮏᩥἲ䛿኱䛝䛟ኚ໬ • ኚᩘ䞉㛵ᩘᐉゝ䛾ᆺ䛿ᚋ⨨ • var message string = hello • ኚᩘᐉゝ᫬䛻ᆺ䛾᫂♧䛜୙せ䠄ᆺ᥎ㄽ䠅 • message := hello • 䝹䞊䝥䛿forᩥ䛾䜏 • ᇶᮏ᪉㔪䠖䝅䞁䝥䝹䛛䛴ᚲせ༑ศ䛺ゝㄒ䝉䝑䝖
  6. 6. ゝㄒ௙ᵝ䠄⥆䛝䠅 • 䜽䝷䝇㝵ᒙ䛜Ꮡᅾ䛧䛺䛔 • ⥅ᢎ䛜Ꮡᅾ䛧䛺䛔 • ⡆᫆䛺ጤㆡ䛾௙⤌䜏䛜ᥦ౪䛥䜜䛶䛔䜛 • 」ᩘᡠ䜚್䚸」ᩘྠ᫬௦ධ • a, b = b, a • 䝫䜲䞁䝍₇⟬䛿Ꮡᅾ䛧䛺䛔 • 㓄ิ䛻ᑐ䛩䜛䜰䝗䝺䝇ຍ⟬䛺䛹䛿䛷䛝䛺䛔 • Slice • ᅛᐃ㛗㓄ิ䜢䝷䝑䝥䛧䛯⤌䜏㎸䜏ᆺ • 䝯䝰䝸㡿ᇦ䛾෌☜ಖ䛺䛹䜢ᐜ᫆䛻⾜䛘䜛 • ➨୍⣭㛵ᩘ • 䜽䝻䞊䝆䝱䛜స䜜䜛 • 䝁䞊䝹䝞䝑䜽㛵ᩘ䛺䛹 • Interface䜢⏝䛔䛯DuckTyping䛾ᐇ⌧ • ୪ิฎ⌮䜢ᐜ᫆䛻䛩䜛ᶵ⬟ • Goroutine, Channel
  7. 7. 䛔䜟䜖䜛䛂䜽䝷䝇䛃䛾⾲⌧ type!Vertex!struct!{! !!X,!Y!float64! }! !! func!(v!*Vertex)!Abs()!float64!{! !!return!math.Sqrt(v.X*v.X!+!v.Y*v.Y)! }! !! func!main()!{! !!v!:=!Vertex{3,!4}! !!fmt.Println(v.Abs())! }! struct 䠇 䝯䝋䝑䝗
  8. 8. Interface • ኱యJava䛸ྠ䛨 • ᢳ㇟䛸ල㇟䜢ศ㞳 • 䝫䝸䝰䞊䝣䜱䝈䝮䜢ᐇ⌧ • ᐇ⿦ᆺ䛷implementsᐉゝ䛿୙せ • DuckTyping䜢ᐇ⌧ 䜲䞁䝍䞊䝣䜵䞊䝇ᐉゝ䠖䝯䝋䝑䝗ᆺᐃ⩏䛾⨶ิ type!Abser!interface!{! !!Abs()!float64! }!
  9. 9. ᐇ⿦ᆺ • ᚲせ䛺䝯䝋䝑䝗䜢ᐇ⿦䛩䜛䛰䛡 Vertexᵓ㐀య(෌ᥖ) type!Vertex!struct!{! !!X,!Y!float64! }! !! func!(v!*Vertex)!Abs()!float64!{! !!return!math.Sqrt(v.X*v.X!+!v.Y*v.Y)! }! 䜲䞁䝍䞊䝣䜵䞊䝇Abser䜢ᐇ⿦䛧䛶䛔䜛䛸䜏䛺䛥䜜䜛
  10. 10. Interfaceᆺ䛾฼⏝ • Interfaceᆺ䜢ᘬᩘ䛻ྲྀ䜛㛵ᩘ䜢ᐃ⩏䛩䜛䛸... func!showAbs(a!Abser)!{! !!fmt.Println(a.Abs())! }! • Vertexᆺ䛰䛡䛨䜓䛺䛟䚸Abs()䜢ᐇ⿦䛧䛯ᆺ䛿䛺䜣䛷䜒ධ䜜䜙䜜 䜛 㟼ⓗ䛺DuckTyping䜢ᐇ⌧
  11. 11. ୪ิฎ⌮ • Goroutine(䝂䝹䞊䝏䞁䠅䛸䛔䛖௙⤌䜏䜢౑䛖 • 䝇䝺䝑䝗䛾䜘䛖䛺䜒䛾 ㏲ḟฎ⌮ ୪ิฎ⌮ Foo() Bar() Baz() go Foo() go Bar() Baz() Foo(), Bar()䛿䛭䜜䛮䜜᪂䛧䛔 Goroutine䛷୪ิᐇ⾜䛥䜜䜛
  12. 12. Goroutine䛸䛿 • ᴫ䛽䝇䝺䝑䝗䛾䜘䛖䛺䜒䛾 • Goroutine != 䝇䝺䝑䝗 • 䝇䝺䝑䝗䜢䛥䜙䛻ศ๭䛧䛯䜒䛾 • ୍䛴䛾䝇䝺䝑䝗䛻」ᩘ䛾Goroutine • ㍍㔞 • 䛯䛟䛥䜣❧䛱ୖ䛢䜙䜜䜛 䝥䝻䝉䝇 䝇䝺䝑䝗 䝇䝺䝑䝗 Goroutine Goroutine
  13. 13. Goroutine㛫䛾㏻ಙ • 1. ඹ᭷䝯䝰䝸䜢౑䛖 • Mutex䛜⏝ព䛥䜜䛶䛔䜛 • 2. Channel䛸䛔䛖௙⤌䜏䜢౑䛖 • Go䛜᥎ዡ䛩䜛୪⾜ฎ⌮䝰䝕䝹 • 䝯䝑䝉䞊䝆䝟䝑䝅䞁䜾 • Go᭣䛟䠖Do not communicate by sharing memory; instead, share memory by communicating 䝯䝰䝸ඹ᭷䛻䜘䛳䛶㏻ಙ䛩䜛䛾䛷䛿䛺䛟䚸㏻ಙ䛻䜘䛳䛶䝯䝰䝸䜢ඹ᭷䛧䜝
  14. 14. Channel • ್䛾஺᥮䛸ྠᮇ䜢ᢸ䛳䛶䛟䜜䜛 func RoutineA(c chan int) { ... c -­‐ v ... } func RoutineB(c chan int) { ... var v := -­‐c ... } RoutineA䛜 c - v 䜢ᐇ⾜䛩䜛䜎䛷 䝤䝻䝑䜽䛥䜜䜛 channel c
  15. 15. 䜎䛸䜑 • Goゝㄒ䛿C/C++䛻ືⓗゝㄒ䛾ὶ⾜ᢏ⾡䜢ྲྀ䜚㎸䜣䛰 • ㏿ᗘ䛜せồ䛥䜜䜛ప䝺䜲䝲䛾䝅䝇䝔䝮䛷⏕⏘ᛶ䛜ୖ䛜䜚䛭䛖 • Docker䛿Go䛷᭩䛛䜜䛶䛔䜛 • Web䜰䝥䝸䜿䞊䝅䝵䞁䛸䛾ぶ࿴ᛶ䛿୙᫂ • Ruby䛸ẚ䜉䛶ඃ఩ᛶ䛿↓䛔䜘䛖䛻ぢ䛘䜛
  16. 16. ⤊䜟䜚 ㉁␲ᛂ⟅

×