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

Study Golang by developing mini crawler

Study Golang by developing mini crawler

Published at gocon 2018 spring

Bo0km4n

April 15, 2018
Tweet

More Decks by Bo0km4n

Other Decks in Programming

Transcript

  1. ࣗݾ঺հ ɹ 2 •Name := ઒෦উ໵ •Twitter := @KKawabe108 •GitHub

    := @Bo0km4n ✦ ॴଐ - ࣳӜ޻ۀେֶେֶӃ ిؾిࢠ৘ใ޻ֶઐ߈ M1 - גࣜձࣾ3-shake (Πϯλʔϯ) ✦ ීஈͷ׆ಈ - 3-shakeͰDMPͷόοΫΤϯυΛgoͰΨϦΨϦॻ͍ͯ·͢ - ࠷ۙ͸झຯͰ8086ͷσΟεΞηϯϒϥΛgoͰ࣮૷ͨ͠Γ
  2. ̍ஈ֊໨ 10 Ϋϩʔϥʔͷ࢓༷ <a href="hoge/fuga.html"> <img src="img/gocon.png"> <script src="https://gocon/src.js"></script> <link

    rel="stylesheet" type="text/css" href="gocon.css"> <a href="/Users/gocon/hoge/fuga.html"> <img src="/Users/gocon/img/gocon.png"> <script src="/Users/gocon/src.js"></script> <link rel="stylesheet" type="text/css" href="/Users/gocon.css"> Replace ύεΛFQDNʹඥ͚ͮͯμ΢ϯϩʔυ͢Δ
  3. ̎ஈ֊໨ 12 $ go run main.go -root https://golang.org/doc/ -depth 2

    Root (Depth 0) Depth 1 Depth 2 rootͷhtmlʹషΒΕ͍ͯΔ ϦϯΫͷϖʔδΛ Ϋϩʔϧ͢Δ ى఺ͱͳΔϖʔδ͸ https://golang.org/doc Ϋϩʔϧ͢Δਂ͞͸̎ Ϋϩʔϥʔͷ࢓༷
  4. ̏ஈ֊໨ Ϋϩʔϥͷ࢓༷ 14 cpus := runtime.NumCPU() runtime.GOMAXPROCS(cpus) c := make(chan

    bool, cpus) for i := 0; i < ֊૚਺; i++ { var wg sync.WaitGroup for _, p := range webϖʔδͷ഑ྻ { c <- true wg.Add(1) go func(p Page) { defer func() { <-c }() // ΫϩʔϦϯά wg.Done() }(p) } wg.Wait() } goroutineΛ༻͍Δ͜ͱͰൺֱత؆୯ʹ ฒྻॲཧΛ࣮૷Ͱ͖Δ ※ go func()಺ͷॲཧ͕͍ܰͱ Ή͠Ζ஗͘ͳΔՄೳੑ΋͋Δ ※ ੜ੒͢Δgoroutineͷ਺΋੍ޚ͠ͳ͍ͱ Ϋϩʔϧ͢Δϖʔδ͕ଟ͍৔߹panic͢Δ͜ͱ΋͋Δ ͔͠͠
  5. ·ͱΊ 17 Go binary Goroutine Goroutine Goroutine Web Page Web

    Page Web Page HTML Img Css JS HTML Img Css JS HTML Img Css JS Download Download Download