Upgrade to Pro — share decks privately, control downloads, hide ads and more …

今あらためて読み直したい Go 基礎知識 その2 / golang.tokyo #25

今あらためて読み直したい Go 基礎知識 その2 / golang.tokyo #25

Masayuki Izumi

June 18, 2019
Tweet

More Decks by Masayuki Izumi

Other Decks in Programming

Transcript

  1. ©2019 Wantedly, Inc.
    ࠓ͋ΒͨΊͯಡΈ௚͍ͨ͠
    Goجૅ৘ใͦͷ
    golang.tokyo #25
    Jun 18, 2019 - @izumin5210

    View full-size slide

  2. ©2019 Wantedly, Inc.
    @izumin5210
    Application Engineer, Wantedly People
    Wantedly, Inc.
    ‣ ͓΋ʹ#BDLFOE&OHJOFFSΛ͍ͯ͠·͢
    ‣ (Pͱ3VCZͱɺ·Εʹ8FCϑϩϯτΤϯυ
    ‣ ࠷ۙͷ͓͖ʹ͍Γ͸HPMBOHPSHYUPPMTHPQBDLBHFTQBDLBHFTUFTU

    View full-size slide

  3. ©2019 Wantedly, Inc.
    About "Specification and Internal"

    View full-size slide

  4. ©2019 Wantedly, Inc.
    Go Data Structures: Interfaces
    Go's interfaces—static, checked at compile time, dynamic when asked for—
    are, for me, the most exciting part of Go from a language design point of view.
    If I could export one feature of Go into other languages, it would be interfaces.
    Page Title Page Subtitle
    https://research.swtch.com/interfaces

    View full-size slide

  5. ©2019 Wantedly, Inc.
    Go Data Structures: Interfaces
    https://research.swtch.com/interfaces
    ‣ 3VTT$PY !STD
    ʹΑΔJOUFSGBDFͷղઆʢʣ
    (Pͷൃද͕Β͍͠
    ‣ લ൒ͰJOUFSGBDFͷ࢖͍ํͷղઆ
    FH
    ίϯύΠϧ࣌ܕνΣοΫͭͭ͠%VDL5ZQJOHͰ͖Δʂ
    EZOBNJDͰ΋ܕνΣοΫͰ͖Δʂʢ5ZQFTXJUDIFTʣ
    ೥લͷهࣄ͚ͩͲࠓݟͯ΋ҧ࿨ײͳ͍
    ‣ ޙ൒͸JOUFSGBDFͷσʔλߏ଄΍࣮૷ʹ͍ͭͯ
    ࢓༷ɾػೳղઆ Go Data Structures: Interfaces

    View full-size slide

  6. ©2019 Wantedly, Inc.
    Interfaceͷσʔλߏ଄
    ‣ *OUFSGBDF͕ͲͷΑ͏ʹ࣮ͯ͠ݱ͞Ε͍ͯΔ͔
    IUUQTHJUIVCDPNHPMBOHHPCMPCHPͱ͔΍Δͱ

    งғؾ͍ۙίʔυ͕ग़ͯ͘Δ
    ‣ σʔλߏ଄
    ӈਤΈ͍ͨͳ࿩
    ͋ΘͤͯಡΈ͍ͨ(P%BUB4USVDUVSFT

    ࠓͩͱTSDSVOUJNF\JGBDF SVOUJNF^HP͋ͨΓ
    ‣ ಺෦࣮૷ʢ௥͍͖Εͣʜʣ
    ࢓༷ɾػೳղઆ Go Data Structures: Interfaces
    * https://research.swtch.com/godata

    View full-size slide

  7. ©2019 Wantedly, Inc.
    Go Slices: usage and internals
    https://blog.golang.org/go-slices-usage-and-internals
    ‣ "OESFX(FSSBOE BEH
    ʹΑΔTMJDFͷղઆʢʣ
    ͜ͷ࣌఺Ͱ͸·ͩ(P͸ʹͳ͍ͬͯͳ͍
    ‣ લ൒Ͱ"SSBZ͓Αͼ4MJDFͷ࢖͍ํͷղઆ
    FHॳظԽ NBLF MFO DBQ ΞΫηεͷ࢓ํ
    ঺հ͞ΕͯΔίʔυશ෦ࠓͰ΋ಈ͘
    ‣ ޙ൒͸TMJDFͷσʔλߏ଄ʹ͍ͭͯ
    ࢓༷ɾػೳղઆ Go Slices: usage and internals

    View full-size slide

  8. ©2019 Wantedly, Inc.
    Sliceͷσʔλߏ଄ɾ࣮૷
    ‣ 4MJDFͷσʔλߏ଄ͷ࿩
    ಺෦తʹ͸BSSBZ΁ͷQPJOUFS MFO DBQΛ΋͍ͬͯΔ
    ʜΈ͍ͨͳ࿩
    ‣ CVJMUJOؔ਺ͷߟ͑ํ
    ʮTMJDFͷDBQΛ૿΍͢ʯʹ͸ɺഒͷDBQΛ࣋ͭ

    TMJDFΛ࡞Γ௚ͤ͹͍͍ΑͶDPQZ
    ʮTMJDF΁ͷ஋௥Ճʯʹ͸ɺద੾ʹDBQ૿΍ͭͭ͠

    ຤ඌʹDPQZ͢Ε͹͍͍ΑͶBQQFOE
    ࢓༷ɾػೳղઆ
    s = s[2:4]
    s = s[:cap(s)]
    Go Slices: usage and internals

    View full-size slide

  9. ©2019 Wantedly, Inc.
    Arrays, slices (and strings): The mechanics of 'append'
    https://blog.golang.org/slices
    ‣ 3PC1JLFʹΑΔTMJDF·ΘΓͷղઆʢʣ
    (PϦϦʔε͔Β೥ޙ͘Β͍
    ‣ ʮॳֶऀ͕ʢଞͷݴޠ͔Βͷઌೖ؍Ͱʁʣ4MJDFͭ·͖͕ͣͪʯ

    ͳͷͰɺBQQFOEͷڍಈ͔Βඥղ͍͍ͯ͘
    ‣ "SSBZ 4MJDFͷղઆΛͯ͠ɺ4MJDFʹ͍ͭͯ۷ΓԼ͍͛ͯ͘
    "TMJDFJTOPUBOBSSBZ"TMJDFEFTDSJCFTBQJFDFPGBOBSSBZ
    ࢓༷ɾػೳղઆ Arrays, slice (and strings): The mechanics of 'append'

    View full-size slide

  10. ©2019 Wantedly, Inc.
    Slice ͷৼΔ෣͍͔ΒڍಈΛཧղ͢Δ
    ‣ 4MJDFͷ಺෦ߏ଄Λཧղ͢Δ
    4MJDFT5IFTMJDFIFBEFS
    ؔ਺ͷҾ਺΍ϨγʔόʹεϥΠεΛೖΕͯΈͯɺ಺෦ߏ଄ͷཧղͷॿ͚ʹ
    ‣ (P4MJDFTVTBHFBOEJOUFSOBMTʹ͋ͬͨΑ͏ͳྲྀΕ
    4MJDFʹ͸࣮͸DBQ͕͋Δ
    େ͖͍4MJDFΛ࡞ΔͨΊʹNBLF͕͋Δ
    4MJDFΛFYUFOE͢Δେ͖͍4MJDFʹDPQZ͢Δ
    ͜ΕΒΛ૊Έ߹ΘͤͯBQQFOEΛ࣮ݱ͢Δ
    ‣ ͋ͱ͸OJMTMJDF΍TUSJOHͷ࿩
    ࢓༷ɾػೳղઆ Arrays, slice (and strings): The mechanics of 'append'
    // Append appends the elements to the slice.
    // Efficient version.
    func Append(slice []int, elements ...int) []int {
    n := len(slice)
    total := len(slice) + len(elements)
    if total > cap(slice) {
    // Reallocate. Grow to 1.5 times the new size,
    newSize := total*3/2 + 1
    newSlice := make([]int, total, newSize)
    copy(newSlice, slice)
    slice = newSlice
    }
    slice = slice[:total]
    copy(slice[n:], elements)
    return slice
    }
    Snippet from https://blog.golang.org/slices

    View full-size slide

  11. ©2019 Wantedly, Inc.
    Constants
    https://blog.golang.org/constants
    ‣ 3PC1JLFʹΑΔDPOTUͷղઆʢʣ
    (PϦϦʔεͷϲ݄ޙ͘Β͍
    ‣ ม਺ͱ͸ҧ͏ɺzී௨ͷ਺ࣈzͬΆ͘ৼΔ෣͏ఆ਺ʹ͍ͭͯ
    qPBUJOU͸μϝɺJOUJOU΋μϝ
    Ͱ΋A1e6 * time.SecondA͸ڐ͞ΕΔ
    ‣ $Ͱ͸͍ΖΜͳʮ਺஋ʯΛࠞͥΒΕΔ͕ɺCVHʹ΋ʜ

    (PͰ͸ېࢭʹ͍ͨ͠ɺ͚ͲAJOU
    AΈ͍ͨͳͷ͸ΊΜͲ͍ʜ
    ࢓༷ɾػೳղઆ Constants

    View full-size slide

  12. ©2019 Wantedly, Inc.
    Constants
    ‣ ADPOTUIFMMP)FMMP ੈքAͷ࣌఺Ͱ͸·ͩTUSJOHͰ͸ͳ͍
    6OUZQFETUSJOHDPOTUBOUͰ͋Δ
    ʢDPOTUBOUUFYUVBMWBMVFͱ΋දݱ͍ͯͨ͠ʣ
    ‣ ͜ͷ͋ͱ%FGBVMUUZQF͔Βܕͷܾఆͷ࿩ʹҠ͍ͬͯ͘
    ‣ ͍࣍ͰTUSJOHҎ֎ͷܕCPPMFBO qPBU ͱଓ͘
    ‣ ࠷ޙʹ਺஋ɺ*OUFHFS qPBU DPNQMFYͱ͢΂ͯΛ಺แ͢Δ࿩
    ࢓༷ɾػೳղઆ Constants

    View full-size slide

  13. ©2019 Wantedly, Inc.
    The Go Memory Model
    https://golang.org/ref/mem
    ‣ (PͷϝϞϦϞσϧͷղઆʢʣ
    ͋ΔHPSPVUJOFͰॻ͖ࠐΈ͕͋ͬͨม਺Λ

    ผͷHPSPVUJOFͰಡΈऔΔ͜ͱ͕Ͱ͖Δ͜ͱʹ͍ͭͯ
    ࢓༷ɾػೳղઆ The Go Memory Model
    *GZPVNVTUSFBEUIFSFTUPGUIJTEPDVNFOUUPVOEFSTUBOE
    UIFCFIBWJPSPGZPVSQSPHSBN ZPVBSFCFJOHUPPDMFWFS


    %POUCFDMFWFS

    View full-size slide

  14. ©2019 Wantedly, Inc.
    Happens before
    ‣ ·ͣ)BQQFOTCFGPSFͱ͍͏ؔ܎Λఆ͍ٛͯ͠Δ
    FHಡΈࠐΈॲཧS͕W΁ͷॻ͖ࠐΈॲཧXΛ؍ଌ͢Δʹ͸

    SEPFTOPUIBQQFOTCFGPSFXͰͳ͍ͱ͍͚ͳ͍
    ԿΛอূ͢Δ͜ͱͰσʔλڞ༗Λ࣮ݱ͍ͯ͠Δ͔Λղઆ͢Δ
    ‣ ӈͷྫʢDIBOOFMʣͰ͋Ε͹
    5IFXSJUFUPBIBQQFOTCFGPSFUIFTFOEPOD
    5IFTFOEPODIBQQFOTCFGPSFUIFSFDFJWFPOD
    5IFSFDFJWFPODIBQQFOTCFGPSFUIFQSJOU
    ࢓༷ɾػೳղઆ Go Slices: usage and internals
    var c = make(chan int, 10)
    var a string
    func f() {
    a = "hello, world"
    c <- 0
    }
    func main() {
    go f()
    <-c
    print(a)
    }

    View full-size slide

  15. ©2019 Wantedly, Inc.
    Happens before
    ‣ ·ͣ)BQQFOTCFGPSFͱ͍͏ؔ܎Λఆ͍ٛͯ͠Δ
    FHಡΈࠐΈॲཧS͕W΁ͷॻ͖ࠐΈॲཧXΛ؍ଌ͢Δʹ͸

    SEPFTOPUIBQQFOTCFGPSFXͰͳ͍ͱ͍͚ͳ͍
    ԿΛอূ͢Δ͜ͱͰσʔλڞ༗Λ࣮ݱ͍ͯ͠Δ͔Λղઆ͢Δ
    ‣ ӈͷྫʢDIBOOFMʣͰ͋Ε͹
    5IFXSJUFUPBIBQQFOTCFGPSFUIFTFOEPOD
    5IFTFOEPODIBQQFOTCFGPSFUIFSFDFJWFPOD
    5IFSFDFJWFPODIBQQFOTCFGPSFUIFQSJOU
    ࢓༷ɾػೳղઆ Go Slices: usage and internals
    var c = make(chan int, 10)
    var a string
    func f() {
    a = "hello, world"
    c <- 0
    }
    func main() {
    go f()
    <-c
    print(a)
    }

    View full-size slide

  16. ©2019 Wantedly, Inc.
    Happens before
    ‣ ·ͣ)BQQFOTCFGPSFͱ͍͏ؔ܎Λఆ͍ٛͯ͠Δ
    FHಡΈࠐΈॲཧS͕W΁ͷॻ͖ࠐΈॲཧXΛ؍ଌ͢Δʹ͸

    SEPFTOPUIBQQFOTCFGPSFXͰͳ͍ͱ͍͚ͳ͍
    ԿΛอূ͢Δ͜ͱͰσʔλڞ༗Λ࣮ݱ͍ͯ͠Δ͔Λղઆ͢Δ
    ‣ ӈͷྫʢDIBOOFMʣͰ͋Ε͹
    5IFXSJUFUPBIBQQFOTCFGPSFUIFTFOEPOD
    5IFTFOEPODIBQQFOTCFGPSFUIFSFDFJWFPOD
    5IFSFDFJWFPODIBQQFOTCFGPSFUIFQSJOU
    ࢓༷ɾػೳղઆ Go Slices: usage and internals
    var c = make(chan int, 10)
    var a string
    func f() {
    a = "hello, world"
    c <- 0
    }
    func main() {
    go f()
    <-c
    print(a)
    }

    View full-size slide

  17. ©2019 Wantedly, Inc.
    Happens before
    ‣ ·ͣ)BQQFOTCFGPSFͱ͍͏ؔ܎Λఆ͍ٛͯ͠Δ
    FHಡΈࠐΈॲཧS͕W΁ͷॻ͖ࠐΈॲཧXΛ؍ଌ͢Δʹ͸

    SEPFTOPUIBQQFOTCFGPSFXͰͳ͍ͱ͍͚ͳ͍
    ԿΛอূ͢Δ͜ͱͰσʔλڞ༗Λ࣮ݱ͍ͯ͠Δ͔Λղઆ͢Δ
    ‣ ӈͷྫʢDIBOOFMʣͰ͋Ε͹
    5IFXSJUFUPBIBQQFOTCFGPSFUIFTFOEPOD
    5IFTFOEPODIBQQFOTCFGPSFUIFSFDFJWFPOD
    5IFSFDFJWFPODIBQQFOTCFGPSFUIFQSJOU
    ࢓༷ɾػೳղઆ Go Slices: usage and internals
    var c = make(chan int, 10)
    var a string
    func f() {
    a = "hello, world"
    c <- 0
    }
    func main() {
    go f()
    <-c
    print(a)
    }

    View full-size slide

  18. ©2019 Wantedly, Inc.
    Buffered chan, Lock, Once
    ‣ #V⒎FSSFE$IBOOFMͷৼΔ෣͍΍ͦΕΛԠ༻ͨ͠

    ಉ࣮࣌ߦ਺੍ݶ͖ͭXPSLFS΍ɺ

    -PDL΍0ODF͕ʮͳʹΛอূͯ͘͠ΕΔ͔ʯͷ

    ղઆ͕͋Δ
    ‣ ϖʔδ಺ΛCFGPSF΍HVBSBOUFFͰHSFQ͢ΔͱಡΈ΍͍͢
    ࢓༷ɾػೳղઆ Go Slices: usage and internals
    var l sync.Mutex
    var a string
    func f() {
    a = "hello, world"
    l.Unlock()
    }
    func main() {
    l.Lock()
    go f()
    l.Lock()
    print(a)
    }
    var a string
    var once sync.Once
    func setup() {
    a = "hello, world"
    }
    func doprint() {
    once.Do(setup)
    print(a)
    }
    func twoprint() {
    go doprint()
    go doprint()
    }
    Lock Once

    View full-size slide

  19. ©2019 Wantedly, Inc.
    Diagnostics
    https://golang.org/doc/diagnostics.html
    ‣ (PΤίγεςϜ͕ఏڙ͢Δ

    UPPMTUPEJBHOPTFMPHJDBOEQFSGPSNBODFQSPCMFNT·ͱΊ
    ΧςΰϦʹ෼ྨͯ͠ղઆ͢Δ
    1SPpMJOH
    5SBDJOH
    %FCVHHJOH
    3VOUJNFTUBUJTUJDTBOEFWFOUT
    ࢓༷ɾػೳղઆ Diagnostics

    View full-size slide

  20. ©2019 Wantedly, Inc.
    Profiling
    net/http/pprof & runtime/pprof
    ‣ ʮॏ͍ͨίʔυʯʮେྔʹݺ͹ΕΔίʔυʯΛ୳͢ॿ͚ʹͳΔ
    औΕΔσʔλʢGSPNIUUQTHPMBOHPSHQLHSVOUJNFQQSPG1SPpMFʣ
    - goroutine - stack traces of all current goroutines
    - heap - a sampling of memory allocations of live objects
    - allocs - a sampling of all past memory allocations
    - threadcreate - stack traces that led to the creation of new OS threads
    - block - stack traces that led to blocking on synchronization primitives
    - mutex - stack traces of holders of contended mutexes
    ‣ 1SPEVDUJPOTBGF͚ͩͲɺଟগ͸σάϨ͢ΔΑɻNVMUJSFQMJDBͷ͏ͪͷQSPDFTT͚ͩʹ͠Α͏Ͷɻ
    ʜΈ͍ͨͳ࢖͍ํͷίπΈ͍ͨͳͷ΋ॻ͍ͯ͋Δ
    ࢓༷ɾػೳղઆ Diagnostics

    View full-size slide

  21. ©2019 Wantedly, Inc.
    Profiling
    ࢓༷ɾػೳղઆ Diagnostics
    Images are from https://golang.org/doc/diagnostics.html#profiling

    View full-size slide

  22. ©2019 Wantedly, Inc.
    Profiling
    ࢓༷ɾػೳղઆ Diagnostics
    Images are from https://golang.org/doc/diagnostics.html#profiling

    View full-size slide

  23. ©2019 Wantedly, Inc.
    Profiling
    ࢓༷ɾػೳղઆ Diagnostics
    Images are from https://golang.org/doc/diagnostics.html#profiling

    View full-size slide

  24. ©2019 Wantedly, Inc.
    Profiling
    ࢓༷ɾػೳղઆ Diagnostics
    Images are from https://golang.org/doc/diagnostics.html#profiling

    View full-size slide

  25. ©2019 Wantedly, Inc.
    Tracing
    golang.org/x/net/trace, net/http/httptrace, ...
    ‣ ʮ͔͜͜Β͜͜·ͰͰԿNT͔͔ͬͨʯΈ͍ͨͳܭଌ
    ΞϓϦʹด͡Δ΋ͷ΍ɺෳ਺ΞϓϦʹ·͕ͨΔ΋ͷͳͲʢ෼ࢄτϨʔγϯάʣ
    USBDFPCKFDU͸DPOUFYU$POUFYUʹ͍Εͯ఻ൖͤ͞Α͏ʂΈ͍ͨͳϓϥΫςΟε΋ॻ͍ͯ͋Δ
    ࢓༷ɾػೳղઆ Diagnostics

    View full-size slide

  26. ©2019 Wantedly, Inc.
    Debugging
    Delve, GDB and core dump!!
    ‣ ίʔυதʹϒϨʔΫϙΠϯτΛஔ͍ͯɺ

    ͕࣮ͦ͜ߦ͞ΕΔλΠϛϯάͷม਺ΛΈͨΓؔ਺Λεςοϓ࣮ߦͨ͠ΓʜΈ͍ͨͳΞϨ
    ‣ AHPCVJMEAʹϑϥάΛ଍͢͜ͱͰ࠷దԽΛݮΒ͠ɺσόοά͠΍͘͢Ͱ͖ΔΑΈ͍ͨͳ࿩͕͋Δ
    ‣ A(053"$"$,DSBTIAʹͯ͠ϓϩάϥϜΛ࣮ߦ͠ɺ4*("#35Λ౤͛ΔͱDPSFEVNQు͔ͤΔ͜ͱ΋

    Ͱ͖ΔΒ͍͠
    ࢓༷ɾػೳղઆ Diagnostics

    View full-size slide

  27. ©2019 Wantedly, Inc.
    Runtime statistics and events
    runtime, runtime/debug
    ‣ SVOUJNFʹऔΕΔॾʑͷ਺ࣈ
    - runtime.ReadMemStats
    - debug.ReadGCStats
    - debug.Stack
    - debug.WriteHeapDump
    - runtime.NumGoroutine
    ‣ SVOUJNFͷΠϕϯτʢTZTDBMM΍($ ͦͷଞ͍Ζ͍ΖʣΛ

    AHPUPPMUSBDFAίϚϯυͰWJTVBMJ[FͰ͖Δ
    ࢓༷ɾػೳղઆ Diagnostics

    View full-size slide

  28. ©2019 Wantedly, Inc.
    About "Concurrency"

    View full-size slide

  29. ©2019 Wantedly, Inc.
    Concurrency
    Page Title Page Subtitle

    View full-size slide

  30. ©2019 Wantedly, Inc.
    Concurrency
    Page Title Page Subtitle

    View full-size slide

  31. ©2019 Wantedly, Inc.
    Concurrency is not parallelism
    https://blog.golang.org/concurrency-is-not-parallelism
    ‣ 3PC1JLFʹΑΔฒྻͱฒߦͷ࿩ʢʣ
    ͜ͷ࣌఺Ͱ͸·ͩ(P͸ʹͳ͍ͬͯͳ͍
    ‣ ͜ͷ࿩
    ฒߦॲཧ Concurrency is not parallelism
    Slide https://talks.golang.org/2012/waza.slide

    View full-size slide

  32. ©2019 Wantedly, Inc.
    Concurrency vs. parallelism
    ฒߦॲཧ Concurrency is not parallelism

    View full-size slide

  33. ©2019 Wantedly, Inc.
    Concurrency vs. parallelism
    ‣ $PODVSSFODZ͸ʢݟ͔͚ͤͰ΋͍͍͔Βʣॲཧ͕ಉ࣌ʹߦ͑ΔΑ͏ͳߏ଄
    ࣮ࡍʹಉ࣌ʹߦΘΕ͍ͯΔ͔͸ؔ܎ͳ͍
    ‣ 1BSBMMFMJTN͸࣮ࡍʹॲཧ͕ಉ࣌ʹߦ͑Δͱ͍͏͜ͱ
    ‣ ؔ৺͕ҧ͏ͷͰɺʮ$PODVSSFODZͰ͋Γ1BSBMMFMJTNͰ΋͋Δʯ΋͋Γ͏Δ
    ※ ॾઆ͋Γ·͢ɻڐͯ͠ɻ
    ฒߦॲཧ Concurrency is not parallelism

    View full-size slide

  34. ©2019 Wantedly, Inc.
    Advanced Go Concurrency Patterns
    https://blog.golang.org/advanced-go-concurrency-patterns
    ‣ 4BNFFS"KNBOJ 4BKNBOJ
    ʹΑΔฒߦॲཧͷ࣮ફྫͷ঺հʢʣ
    HPSPVUJOFDIBOOFMTFMFDUͳͲΛ;ΜͩΜʹ࢖ͬͨྫ
    ฒߦॲཧ Advanced Go Concurrency Patterns
    Slide https://talks.golang.org/2013/advconc.slide

    View full-size slide

  35. ©2019 Wantedly, Inc.
    ฒߦॲཧ Advanced Go Concurrency Patterns
    Snippets are from https://talks.golang.org/2013/advconc.slide
    for {
    if s.closed {
    close(s.updates)
    return
    }
    items, next, err := s.fetcher.Fetch()
    if err != nil {
    s.err = err
    time.Sleep(10 * time.Second)
    continue
    }
    for _, item := range items {
    s.updates <- item
    }
    if now := time.Now(); next.After(now) {
    time.Sleep(next.Sub(now))
    }
    }
    func (s *naiveSub) Close() error {
    s.closed = true
    return s.err
    }
    ‣ ࠨͷίʔυʹ͸όά͕Օॴ͋Γ·͢ɻͲ͜Ͱ͠ΐ͏ʁ

    View full-size slide

  36. ©2019 Wantedly, Inc.
    ฒߦॲཧ Advanced Go Concurrency Patterns
    Snippets are from https://talks.golang.org/2013/advconc.slide
    for {
    if s.closed {
    close(s.updates)
    return
    }
    items, next, err := s.fetcher.Fetch()
    if err != nil {
    s.err = err
    time.Sleep(10 * time.Second)
    continue
    }
    for _, item := range items {
    s.updates <- item
    }
    if now := time.Now(); next.After(now) {
    time.Sleep(next.Sub(now))
    }
    }
    func (s *naiveSub) Close() error {
    s.closed = true
    return s.err
    }
    ‣ ࠨͷίʔυʹ͸όά͕Օॴ͋Γ·͢ɻͲ͜Ͱ͠ΐ͏ʁ
    ‣ ʜΈ͍ͨͳײ͡Ͱɺ࣮ࡍͷίʔυͷόάΛ௵͍͖ͯ͠ͳ͕Β

    ฒߦॲཧΛ࣮૷͢ΔͨΊͷύλʔϯΛ঺հͯ͘͠ΕΔ


    View full-size slide

  37. ©2019 Wantedly, Inc.
    ฒߦॲཧ Advanced Go Concurrency Patterns
    Snippets are from https://talks.golang.org/2013/advconc.slide
    for {
    if s.closed {
    close(s.updates)
    return
    }
    items, next, err := s.fetcher.Fetch()
    if err != nil {
    s.err = err
    time.Sleep(10 * time.Second)
    continue
    }
    for _, item := range items {
    s.updates <- item
    }
    if now := time.Now(); next.After(now) {
    time.Sleep(next.Sub(now))
    }
    }
    func (s *naiveSub) Close() error {
    s.closed = true
    return s.err
    }
    ‣ ࠨͷίʔυʹ͸όά͕Օॴ͋Γ·͢ɻͲ͜Ͱ͠ΐ͏ʁ
    ‣ ʜΈ͍ͨͳײ͡Ͱɺ࣮ࡍͷίʔυͷόάΛ௵͍͖ͯ͠ͳ͕Β

    ฒߦॲཧΛ࣮૷͢ΔͨΊͷύλʔϯΛ঺հͯ͘͠ΕΔ

    ౴͑͸ࣗ෼Ͱݟ͚ͭͯͶ

    View full-size slide

  38. ©2019 Wantedly, Inc.
    Go Concurrency Patterns: Pipelines and cancellation
    https://blog.golang.org/pipelines
    ‣ 4BNFFS"KNBOJ 4BKNBOJ
    ʹΑΔύΠϓϥΠϯॲཧͷ࿩ʢʣ
    ‣ 1JQFMJOFॲཧ͸͍͍ͩͨ͜͏͍͏ͷΛΠϝʔδ͢Δͱྑ͍΍ͭ
    ͜Ε΋࣮ࡍͷίʔυΛྫʹͭͭ͠ɺ

    ඞཁͳཁૉΛ࣮͋͛ͯ૷͍ͯ͘͠
    ฒߦॲཧ Go Concurrency Patterns: Pipelines and cancellation
    Image from https://talks.golang.org/2012/waza.slide

    View full-size slide

  39. ©2019 Wantedly, Inc.
    PipelineॲཧͰߟྀ͢΂͖͜ͱ
    ‣ 'BOPVU
    ‣ 'BOJO
    ‣ ऴྃॲཧ
    ‣ $BODFMॲཧ
    ฒߦॲཧ Go Concurrency Patterns: Pipelines and cancellation

    View full-size slide

  40. ©2019 Wantedly, Inc.
    PipelineॲཧͰߟྀ͢΂͖͜ͱ
    ‣ 'BOPVU
    ॲཧ͢ΔϫʔΧʔͷ਺Λ૿΍͢ʢDGTDBMFPVUʣ
    ‣ 'BOJO
    ฒྻʹॲཧͨ݁͠ՌΛগ਺ͷϫʔΧʔʹू໿ͯ͠ॲཧ
    ‣ ऴྃॲཧ
    ‣ $BODFMॲཧ
    ฒߦॲཧ Go Concurrency Patterns: Pipelines and cancellation

    View full-size slide

  41. ©2019 Wantedly, Inc.
    PipelineॲཧͰߟྀ͢΂͖͜ͱ
    ‣ 'BOPVU
    ‣ 'BOJO
    ‣ ऴྃॲཧ
    ऴྃ͢Δ·͑ʹɺॲཧதͷϝοηʔδ͸શ෦ऴΘΔ·Ͱ଴ͭʢDGHSBDFGVMTIVUEPXOʣ
    ‣ $BODFMॲཧ
    ֎෦͔ΒͷऴྃγάφϧΛड͚औΕΔΑ͏ʹ͠Α͏ʢFH4*(5&3. λΠϜΞ΢τ DGDPOUFYU$POUFYUʣ
    ฒߦॲཧ Go Concurrency Patterns: Pipelines and cancellation

    View full-size slide

  42. ©2019 Wantedly, Inc.
    About "Naming"

    View full-size slide

  43. ©2019 Wantedly, Inc.
    What's in a name?
    https://talks.golang.org/2014/names.slide
    ‣ "OESFX(FSSBOE BEH
    ʹΑΔɺ(Pʹ͓͚Δྑ໊͍લ෇͚ʹؔ͢Δ࿩ʢʣ
    ‣ ʮͲ͏͍͏໊લΛ͚ͭΕ͹Α͍͔ʁʯΛཁૉผʹ঺հ͍ͯ͠Δ
    FH
    ؔ਺ͷҾ਺໊͸υΩϡϝϯτʹͳΔͷͰɺܕ͚ͩઆ໌Ͱ͖ͳ͍৔߹͸ҙຯͷ͋Δ໊લΛ͚ͭΑ͏
    A"GUFS'VOD E%VSBUJPO GGVOD

    5JNFSAA6OJY TFD OTFDJOU
    5JNFA
    ؔ਺ͷϨγʔό໊͸ܕ໊Λ൓өͨ͠ʙจࣈ͘Β͍ʹ͠Α͏
    Naming What's in a name?
    Image from https://talks.golang.org/2012/waza.slide

    View full-size slide

  44. ©2019 Wantedly, Inc.
    Naming What's in a name?

    View full-size slide

  45. ©2019 Wantedly, Inc.
    Package names
    https://blog.golang.org/package-names
    ‣ (Pͷྑ͍ύοέʔδ໊ͷ࿩ʢʣ
    ‣ ʮྑ͍ύοέʔδ໊ʯʮѱ͍ύοέʔδ໊ʯΛ͍ΖΜͳྫΛڍ͛ͭͭઆ໌͍ͯ͘͠
    FH
    ⭕୹ͯ͘Θ͔Γ΍͍͢UJNF MJTU IUUQ
    ❌ޠҎ্࢖Θͳ͍DPNQVUF4FSWJDF$MJFOU QSJPSJUZ@RVFVF

    ⭕ʢઅ౓͋ΔʣུޠTUSDPOW TZTDBMM GNU
    Naming What's in a name?
    Image from https://talks.golang.org/2012/waza.slide

    View full-size slide

  46. ©2019 Wantedly, Inc.
    Package names
    ‣ ίϯςϯπͷ໊લ΍ύοέʔδͷύεͷ໋໊ʹ͍ͭͯ΋ݴٴ͕͋Δ
    FH
    ৑௕ʹ͠ͳ͍❌IUUQ)5514FSWFS ⭕IUUQ4FSWFS
    ᐆດ͕͞ͳ͚Ε͹ύοέʔδ໊͸͔Ϳͬͯ΋ྑ͍SVOUJNFQQSPG OFUIUUQQQSPG
    ‣ ʮ͜Ε͸ྑ͘ͳ໊͍લʂʯνΣοΫϦετ
    FH

    ҙຯͷͳ໊͍લ͸μϝVUJM DPNNPO
    (͢΂ͯͷ"1*Λύοέʔδʹू໿͢Δͷ͸μϝBQJ UZQFT JOUFSGBDFT
    ͜͜Ͱ͍͏"1*͸8FC"1*ͷ͜ͱͰ͸ͳ͍

    ʢҙຯ͕޿͍ͷ͸ͦͷͱ͓Γ͕ͩɺίϯςΩετʹΑͬͯ͸ڐ͞ΕΔͷͰ͸ͱݸਓతʹ͸ߟ͍͑ͯΔʣ
    Naming What's in a name?
    Image from https://talks.golang.org/2012/waza.slide

    View full-size slide

  47. ©2019 Wantedly, Inc.
    References

    View full-size slide

  48. ©2019 Wantedly, Inc.
    References Specifications and internal
    ‣ (P%BUB4USVDUVSFT*OUFSGBDFT
    IUUQTSFTFBSDITXUDIDPNJOUFSGBDFT
    ͋ΘͤͯಡΈ͍ͨ
    HPMBOHHPͷTSDSVOUJNFҎԼ
    (P%BUB4USVDUVSFTIUUQTSFTFBSDITXUDIDPNHPEBUB
    ‣ (P4MJDFTVTBHFBOEJOUFSOBMT
    IUUQTCMPHHPMBOHPSHHPTMJDFTVTBHFBOEJOUFSOBMT
    ͋ΘͤͯಡΈ͍ͨ
    'VSUIFS3FBEJOHʹ͕͋ͬͯΔ΍ͭ
    ‣ "SSBZT TMJDFT BOETUSJOHT
    5IFNFDIBOJDTPGBQQFOE
    IUUQTCMPHHPMBOHPSHTMJDFT
    ͋ΘͤͯಡΈ͍ͨ
    4MJDF5SJDLTIUUQTHPMBOHPSHXJLJ4MJDF5SJDLT

    View full-size slide

  49. ©2019 Wantedly, Inc.
    References Specifications and internal
    ‣ $POTUBOUT
    IUUQTCMPHHPMBOHPSHDPOTUBOUT
    ‣ 5IF(P.FNPSZ.PEFM
    IUUQTHPMBOHPSHSFGNFN
    ͋ΘͤͯಡΈ͍ͨ
    ʰ(PݴޠʹΑΔฒߦॲཧʱʢΰϧʔνϯΛ౬ਫͷΑ͏ʹ࢖͏ຊʣ
    ‣ %JBHOPTUJDT
    IUUQTHPMBOHPSHEPDEJBHOPTUJDTIUNM
    ͋ΘͤͯಡΈ͍ͨ
    (PQQSPGϚελʔฤ QQSPGͷ࢓૊Έ
    ,-BC(BNFT5FDI#MPH

    IUUQLMBCHBNFTUFDICMPHKQLMBCDPNBSDIJWFTQQSPGJOUFSOBMTIUNM
    %4"4։ൃऀͷ෦԰QJYJWQSJWBUFJTVDPO߈ུ

    IUUQETBTCMPHLMBCPSHBSDIJWFTQJYJWJTVDPOIUNM

    View full-size slide

  50. ©2019 Wantedly, Inc.
    References Concurrency
    ‣ $PODVSSFODZJTOPUQBSBMMFMJTN
    IUUQTCMPHHPMBOHPSHDPODVSSFODZJTOPUQBSBMMFMJTNIUUQTUBMLTHPMBOHPSHXB[BTMJEF
    ͋ΘͤͯಡΈ͍ͨ
    ʰ(PݴޠʹΑΔฒߦॲཧʱʢΰϧʔνϯΛ౬ਫͷΑ͏ʹ࢖͏ຊʣ
    ‣ "EWBODFE(P$PODVSSFODZ1BUUFSOT
    IUUQTCMPHHPMBOHPSHBEWBODFEHPDPODVSSFODZQBUUFSOTIUUQTUBMLTHPMBOHPSHBEWDPODTMJEF
    ͋ΘͤͯಡΈ͍ͨ
    ʰ(PݴޠʹΑΔฒߦॲཧʱʢΰϧʔνϯΛ౬ਫͷΑ͏ʹ࢖͏ຊʣ
    ‣ (P$PODVSSFODZ1BUUFSOT1JQFMJOFTBOEDBODFMMBUJPO
    IUUQTCMPHHPMBOHPSHQJQFMJOFT
    ͋ΘͤͯಡΈ͍ͨ
    ࣮ࡍʹύΠϓϥΠϯॲཧɾฒྻॲཧ΍ͬͯͦ͏ͳίʔυʢFHHPPHMFDMPVEHP AHPNPEUJEZAίϚϯυʣ

    View full-size slide

  51. ©2019 Wantedly, Inc.
    References Naming
    ‣ 8IBUTJOBOBNF
    IUUQTUBMLTHPMBOHPSHOBNFTTMJEF
    ͋ΘͤͯಡΈ͍ͨ

    ‣ 1BDLBHFOBNFT
    IUUQTCMPHHPMBOHPSHQBDLBHFOBNFT
    ͋ΘͤͯಡΈ͍ͨ

    View full-size slide