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

DESAMIS Go Training Season 1 Day 5

DESAMIS Go Training Season 1 Day 5

デザミス社内 Go 研修 (第1期) の5日目の資料です。
今回は Go の豊富で協力な標準ライブラリの歩き方 (探し方) を紹介したのち、実際に複数の標準ライブラリを組み合わせて実用的なアプリケーションを作るハンズオンを行います。

Yutaka Kato

May 18, 2021
Tweet

More Decks by Yutaka Kato

Other Decks in Technology

Transcript

  1. ݚमܭը ճ ೔࣍ ༧ఆ λΠτϧ ୈճ  Ր  (Pͷجຊ

    ୈճ  Ր  ؀ڥߏஙͱ(JU (JU)VCͷجຊ ୈճ  Ր  جຊߏจ ୈճ  Ր  ߏ଄ମɺεϥΠεɺϚοϓ ୈճ  Ր  ඪ४ϥΠϒϥϦ͸΍Ί͙Γ ୈճ  Ր  ϞδϡʔϧγεςϜͱ֎෦ϥΠϒϥϦ ୈճ  Ր  )FSPLV(Pೖ໳ ୈճ  Ր  "84-BNCEB(Pೖ໳ ୈճ  Ր  ฒߦॲཧϓϩάϥϛϯά ୈճ  Ր  ୯ମςετٕ๏ ୈճ  Ր  σʔλϕʔεΞΫηε ୈճ  Ր  'ZOFʹΑΔ(6*ΞϓϦέʔγϣϯ։ൃ ୈճ  Ր  ࣾ಺(PϓϩδΣΫτͷίʔυղઆ 
  2. τοϓϨϕϧͷҰཡ  BSDIJWF DSZQUP fl BH JP QMVHJO TZOD CV

    fi P EBUBCBTF GNU MPH SF fl FDU TZTDBMM CVJMUJO EFCVH HP NBUI SFHFYQ UFTUJOH CZUFT FNCFE IBTI NJNF SVOUJNF UFYU DPNQSFTT FODPEJOH IUNM OFU TPSU UJNF DPOUBJOFS FSSPST JNBHF PT TUSDPOW VOJDPEF DPOUFYU FYQWBS JOEFY QBUI TUSJOHT VOTBGF (PͰ௥Ճ (PͰ௥Ճ (PͰ௥Ճ
  3. ࣾ಺ίʔυͷར༻࣮੷  BSDIJWF DSZQUP fl BH JP QMVHJO TZOD CV

    fi P EBUBCBTF GNU MPH SF fl FDU TZTDBMM CVJMUJO EFCVH HP NBUI SFHFYQ UFTUJOH CZUFT FNCFE IBTI NJNF SVOUJNF UFYU DPNQSFTT FODPEJOH IUNM OFU TPSU UJNF DPOUBJOFS FSSPST JNBHF PT TUSDPOW VOJDPEF DPOUFYU FYQWBS JOEFY QBUI TUSJOHT VOTBGF (JU)VCͷίʔυݕࡧͰௐ΂·ͨ͠
  4. ଟ਺ͷαϒσΟϨΫτϦ w BSDIJWFUBSBSDIJWF[JQ w DPNQSFTTC[JQDPNQSFTT fl BUFDPNQSFTTH[JQ DPNQSFTTM[XDPNQSFTT[MJC w DPOUBJOFSIFBQDPOUBJOFSMJTUDPOUBJOFSSJOH

    w DSZQUPBFTDSZQUPDJQIFSDSZQUPEFTDSZQUPETBDSZQUP FDETBDSZQUPFEDSZQUPFMJQUJD 
 ʜ  εϥογϡͰ۠੾ΒΕͨΑΓࡉ͔͍෼ྨ͕͋Δ΋ͷ͕ଟ͍
  5. HPEPDίϚϯυ  FODPEJOHDTWΛௐ΂͍ͨͳˠgo doc csv  ·ͣ͸/FX8SJUFSؔ਺͔ͳʜˠgo doc csv.NewWriter 

    ͭ͗ʹ8SJUFSߏ଄ମΛৄ͘͠ʜˠgo doc csv.Writer  'MVTIϝιουͷ࢓༷͸ʜˠgo doc csv.Writer.Flush go doc <pkg > go doc <sym>[.<methodOrField> ] go doc [<pkg>.]<sym>[.<methodOrField> ] go doc [<pkg>.][<sym>.]<methodOrField> 
  6. (JU)VCͷίʔυ w (Pͷίʔυ͸(JU)VCͰ ΋ ެ։͞Ε͍ͯΔ 
 IUUQTHJUIVCDPNHPMBOHHP w ඪ४ϥΠϒϥϦ͕͋Δͷ͸TSDҎԼ 


    IUUQTHJUIVCDPNHPMBOHHPUSFFNBTUFS TSD w ඪ४ϥΠϒϥϦͷઃܭ΍࣮૷͸ྑ͍͓खຊɺؔ਺ͷ ໋໊ύλʔϯ͔ΒυΩϡϝϯτͷॻ͖ํ·Ͱɺਅࣅ Ͱ͖ΔϙΠϯτ͸ͨ͘͞Μ͋Δ 
  7. 45&1 fl BH package mai n import ( "flag "

    "fmt " ) func main() { areaCode := flag.String("c", "130000", "ΤϦΞίʔυ" ) flag.Parse( ) fmt.Printf("ΤϦΞίʔυ͸ %s Ͱ͢\n", *areaCode ) }  fl BHίϚϯυϥΠϯҾ਺Λศརʹѻ͏ϥΠϒϥϦ  👇ࣸܦͯ͠ΈΑ͏ EBZIBOETPONBJOHP ʮgo run .ʯʮgo run . -c 016000ʯʮgo run . -hʯͷ࣮ߦ݁Ռ͸ʁ
  8. 45&1OFUIUUQ resp, err := http.Get("https://www.jma.go.jp" + "/bosai/forecast/data/overview_forecast/" + *areaCode +

    ".json" ) if err != nil { fmt.Println(err ) os.Exit(1 ) } defer resp.Body.Close( ) fmt.Printf("εςʔλείʔυ͸ %d Ͱ͢\n", resp.StatusCode)  OFUIUUQଟ࠼ͳ)551௨৴͕ՄೳͳϥΠϒϥϦ 
 ·ͣ͸࠷΋؆୯ʹ࢖͑Δ(FU Λ࢖ͬͯΈΔ 👇NBJOؔ਺ͷ຤ඌʹॻ͖଍ͯ͠ΈΑ͏ ʮgo run .ʯ͸ɺʮgo run . -c 123ʯ͸ʹͳΕ͹0,
  9. 45&1JP   body, err := io.ReadAll(resp.Body ) if err

    != nil { fmt.Println(err ) os.Exit(1 ) } fmt.Println(string(body)) JPศརͳ൚༻ೖग़ྗॲཧϥΠϒϥϦ 👇NBJOؔ਺ͷ຤ඌʹॻ͖଍ͯ͠ΈΑ͏ ʮgo run .ʯͷ࣮ߦ݁Ռ͸ʁ
  10. 45&1MPH if err != nil { log.Fatalf("઀ଓͰ͖·ͤΜͰͨ͠: %v", err )

    }  MPHγϯϓϧͳϩάग़ྗϥΠϒϥϦ  if err != nil { fmt.Println(err ) os.Exit(1 ) } 👇ҎԼͷΑ͏ʹॻ͍͍ͯͨͱ͜ΖΛʜ 👇ҎԼͷΑ͏ʹॻ͖׵͑ͯΈ·͠ΐ͏ if err != nil { log.Fatalf("Ϩεϙϯεͷड৴ʹࣦഊ͠·ͨ͠: %v", err ) }
  11. 45&1FODPEJOHKTPO  if resp.StatusCode != http.StatusOK { log.Fatalf("HTTP %s: %v",

    resp.Status, string(body) ) } forecast := struct { HeadlineText string `json:"headlineText" ` Text string `json:"text" ` }{ } if err := json.Unmarshal(body, &forecast); err != nil { log.Fatalf("JSON ͷղੳʹࣦഊ͠·ͨ͠: %v", err ) } log.Println("ʲϔουϥΠϯʳ" + forecast.HeadlineText ) log.Println("ʲຊจʳ" + forecast.Text) FODPEJOHKTPO+40/ύʔαʔˍσίʔμʔ 👇NBJOؔ਺ͷ຤ඌʹॻ͖଍ͯ͠ΈΑ͏ ʮgo run .ʯʮgo run . -c 016000ʯͷ࣮ߦ݁Ռ͸ʁ
  12. 45&1UJNF ReportDatetime time.Time `json:"reportDatetime"`  UJNF೔෇΍࣌ࠁΛѻ͏ͱ͖ʹඞཁʹͳΔϥΠϒϥϦ 
 ॻࣜͷࢦఆ͕ಠಛʜ 👇GPSFDBTUͷߏ଄ମʹϑΟʔϧυΛ଍ͯ͠ΈΑ͏ log.Println("ʲใࠂ࣌ࠁʳ"

    + forecast.ReportDatetime.Format("2006೥01݄02೔ 15࣌04෼")) 👇NBJOؔ਺ͷ຤ඌʹॻ͖଍ͯ͠ΈΑ͏ ʮgo run .ʯʮgo run . -c 016000ʯͷ࣮ߦ݁Ռ͸ʁ
  13. ίʔυྫͷඋߟ  w ࠓճར༻ͨ͠ؾ৅ிͷ63-͸ެࣜͷ"1*Ͱ͸ͳ͘࢓༷ͷܧଓੑ͸อূ͞Ε ͍ͯ·ͤΜ͕ɺ੓෎ඪ४ར༻ن໿ͷԼͰར༻Ͱ͖·͢ 
 ࢀߟIUUQTUXJUUFSDPNF@UPZPEBTUBUVT T  w

    ΤϦΞίʔυ͸ɺҎԼͷϖʔδΛ։͍ͯௐ΂͍ͨ஍఺Λબ୒ޙɺϒϥ΢βʹ දࣔ͞Εͨ63-ͷBSFB@DPEFͱ͍͏ΫΤϦύϥϝʔλ͔Β࠾औͰ͖·͢ 
 👉IUUQTXXXKNBHPKQCPTBJGPSFDBTU w SFTQ#PEZ$MPTF ͸ࣦഊ࣌FSSPSܕ͕ฦ٫͞ΕΔͷͰɺ͜ΕΛर͏ʹ͸ ҎԼͷΑ͏ʹॻ͖·͢ 
 defer func() {
 if err := resp.Body.Close(); err != nil {
 log.Printf(“ೖྗετϦʔϜͷΫϩʔζʹࣦഊ͠·ͨ͠: %v", err)
 }
 }()
  14. DPOUFYUͷొ৔  (P͔ΒDPOUFYUϥΠϒϥϦ͕ొ৔ 
 λΠϜΞ΢τॲཧͳͲͷѻ͍͕ΑΓ໌շʹ url := flag.String("u", "https://www.desamis.co.jp/", "URL"

    ) timeout := flag.Duration("t", 500 * time.Millisecond, "઀ଓλΠϜΞ΢τ" ) flag.Parse( ) ctx, _ := context.WithTimeout(context.Background(), *timeout ) req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil ) if err != nil { panic(err ) } resp, err := new(http.Client).Do(req) // ࢦఆͨ͠λΠϜΞ΢τظؒΛ௒͑Δͱࣦഊ͢Δ σϑΥϧτಈ࡞ʹ೚ͤΔͱ͖͸ context.Background() Λ࢖͏
 ઃܭ్தͩͬͨΓͰఆ·Βͳ͍࣌༻ʹ context.TODO() ΋͋Δ 👇
  15. FNCFEͷొ৔  (P͔ΒFNCFEϥΠϒϥϦ͕ొ৔ 
 ࣮ߦόΠφϦ FYF ʹϑΝΠϧΛαΫοͱຒΊࠐΈʂ package mai n

    import ( _ "embed " "fmt " ) //go:embed memo.jso n var memo []byt e func main() { fmt.Println(string(memo) ) } package mai n import ( "embed " "fmt " ) //go:embed publi c var public embed.F S func main() { memo, _ := public.ReadFile("public/memo.json" ) fmt.Println(string(memo) ) } ϑΝΠϧΛຒΊࠐΉ σΟϨΫτϦΛ·Δ͝ͱຒΊࠐΉ
  16. JPJPVUJMͷ࣮࣭ഇࢭ  (P͔ΒJPJPVUJMͷػೳ͕PTͱJPʹҠ২ 
 ࠓޙ͸ޓ׵ੑͷͨΊʹ࢒͞ΕΔ͚ͩͱͳΔ #FGPSF "GUFS ioutil.Discard io.Discard ioutil.NopCloser

    io.NopCloser ioutil.ReadAll io.ReadAll ioutil.ReadDir os.ReadDir ioutil.ReadFile os.ReadFile ioutil.TempDir os.MkdirTemp ioutil.TempFile os.CreateTemp ioutil.WriteFile os.WriteFile
  17. ࿅श໰୊ w ࿅श໰୊ 1 <ϑΥϧμ໊EBZFY> w ·ͣ͸1ͷGFUDIͷࣸܦ͔Β w ࿅श໰୊ 1

    <ϑΥϧμ໊EBZFY> w ͖͞΄ͲͷϋϯζΦϯͰطʹ௨ͬͨಓͳͷͰ ؆୯Ͱ͢Ͷ 
  18. ίʔυΛॻ͖ऴ͑ͨΒ w ʮϢʔβʔ໊QSBDUJDFʯϒϥϯνͰ࡞ۀ͍ͯ͠Δ͜ͱΛ֬ೝ͍ͯͩ͘͠͞ w 74ίʔυͷҰ൪ࠨԼʹදࣔ͞Ε͍ͯ·͢ w มߋΛεςʔδ͠ɺίϛοτ͍ͯͩ͘͠͞ w 74ίʔυͷࠨϝχϡʔͷʮιʔε؅ཧʯ͔Βૢ࡞ w

    ίʔυΛ(JU)VCʹϓογϡ͍ͯͩ͘͠͞ w 74ίʔυͷҰ൪ࠨԼͷϒϥϯν໊ͷ͙͢ӈʹ͋ΔΞΠίϯ͔Βૢ࡞ ্ਤ  w ϓϧϦΫΤετΛ࡞੒͍ͯͩ͘͠͞ w ʮιʔε؅ཧʯͷ্෦ʹΞΠίϯ͕͋Γ·͢