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

煩雑な運用を
Goを使って楽にする / Go Conference 2019 Spring

Konboi
May 18, 2019

煩雑な運用を
Goを使って楽にする / Go Conference 2019 Spring

GoCon 2019 で発表した 煩雑な運用を
Goを使って楽にする の発表資料になります

Konboi

May 18, 2019
Tweet

More Decks by Konboi

Other Decks in Technology

Transcript

  1. ͸͡Ίʹ • ݱ࣌఺ͰΫʔϙϯσʔλ͸ExcelΛ࢖ͬͯೖߘ • ઐ༻ͷೖߘγεςϜ࡞Δͷ͸ίετେ • Θ͔Γ΍͍͢UI • ηΩϡϦςΟ •

    ద੾ͳݖݶ؅ཧ • γεςϜͷӡ༻ • etc • ϛχϚϜͰݕূͯ͠Ձ஋͕͋ΔͳΒ͖ͪΜͱ੔උ
  2. ௐࠪ/ݕূ • ηϧ݁߹ • ࠨ্ͷηϧͷΈσʔλ͕ೖͬͨঢ়ଶʹ $ go run main.go -f

    ../example.xlsx name,name_en,date,status,id,created_at,category, ฏ੒,heisei,01-08-89,ఱߖ,ฏ੒:heisei,5/18/19 06:53,A, ฏ੒,heisei,04-30-19,ఱߖ,ฏ੒:heisei,5/18/19 06:53,, ྩ࿨,reiwa,05-01-19,্ߖ,ྩ࿨:reiwa,5/18/19 06:53,B,
  3. ௐࠪ/ݕূ • ؔ਺ • දࣔ͞Ε͍ͯΔσʔλ͕औಘͰ͖Δ $ go run main.go -f

    ../example.xlsx name,name_en,date,status,id,created_at,category, ฏ੒,heisei,01-08-89,ఱߖ,ฏ੒:heisei,5/18/19 06:53,A, ฏ੒,heisei,04-30-19,ఱߖ,ฏ੒:heisei,5/18/19 06:53,, ྩ࿨,reiwa,05-01-19,্ߖ,ྩ࿨:reiwa,5/18/19 06:53,B,
  4. ௐࠪ/ݕূ • ؔ਺ • ݱࡏηϧʹೖྗ͞Ε͍ͯΔσʔλΛऔಘ $ date "+%Y/%m/%d %H:%M:%S" 2019/05/18

    06:58:14
 
 $ go run main.go -f ../example.xlsx name,name_en,date,status,id,created_at,category, ฏ੒,heisei,01-08-89,ఱߖ,ฏ੒:heisei,5/18/19 06:53,A, ฏ੒,heisei,04-30-19,ఱߖ,ฏ੒:heisei,5/18/19 06:53,, ྩ࿨,reiwa,05-01-19,্ߖ,ྩ࿨:reiwa,5/18/19 06:53,B,
  5. ௐࠪ/ݕূ • ೖྗܗࣜ - Ϧετ • ݱࡏηϧʹೖྗ͞Ε͍ͯΔσʔλΛऔಘ $ go run

    main.go -f ../example.xlsx name,name_en,date,status,id,created_at,category, ฏ੒,heisei,01-08-89,ఱߖ,ฏ੒:heisei,5/18/19 06:53,A, ฏ੒,heisei,04-30-19,ఱߖ,ฏ੒:heisei,5/18/19 06:53,, ྩ࿨,reiwa,05-01-19,্ߖ,ྩ࿨:reiwa,5/18/19 06:53,B,
  6. ௐࠪ/ݕূ • ॻࣜઃఆ • ϑΥʔϚοτ͕͓͔͍͠ $ go run main.go -f

    ../example.xlsx name,name_en,date,status,id,created_at,category, ฏ੒,heisei,01-08-89,ఱߖ,ฏ੒:heisei,5/18/19 06:53,A, ฏ੒,heisei,04-30-19,ఱߖ,ฏ੒:heisei,5/18/19 06:53,, ྩ࿨,reiwa,05-01-19,্ߖ,ྩ࿨:reiwa,5/18/19 06:53,B,
  7. ௐࠪ/ݕূ • ॻࣜઃఆ • ςΩετ $ go run main.go -f

    ../example.xlsx name,name_en,date,status,id,created_at,category, ฏ੒,heisei,1989/01/08,ఱߖ,ฏ੒:heisei,5/18/19 07:06,A, ฏ੒,heisei,2019/04/30,ఱߖ,ฏ੒:heisei,5/18/19 07:06,, ྩ࿨,reiwa,2019/05/01,্ߖ,ྩ࿨:reiwa,5/18/19 07:06,B,
  8. • Ex) 14: m/d/yy • Ex) 15: d-mmm-yy ௐࠪ/ݕূ ݁Ռ

    $ go run main.go -f ../example.xlsx name,name_en,date,status,id,created_at,category, ฏ੒,heisei,8-Jan-89,ఱߖ,ฏ੒:heisei,5/18/19 07:09,A, ฏ੒,heisei,30-Apr-19,ఱߖ,ฏ੒:heisei,5/18/19 07:09,, ྩ࿨,reiwa,1-May-19,্ߖ,ྩ࿨:reiwa,5/18/19 07:09,B, $ go run main.go -f ../example.xlsx ฏ੒,heisei,01-08-89,ఱߖ,ฏ੒:heisei,5/18/19 07:09,A, ฏ੒,heisei,04-30-19,ఱߖ,ฏ੒:heisei,5/18/19 07:09,, ྩ࿨,reiwa,05-01-19,্ߖ,ྩ࿨:reiwa,5/18/19 07:09,B,
  9. ϑΝΠϧϦετ͸FieldΛࢦఆ • ࢦఆ͠ͳ͍ͱid,name, kind, mimeTypeͷΈ • ˎࢦఆ͢Δͱ͢΂ͯͷσʔλ͕
 ฦͬͯ͘Δ͔ύϑΥʔϚϯε͕ྼԽ • srv.Files.List().

    Fields(“files(parents, id, name, owners, kind, mimeType)"). Do()
 
 srv.Files.List().Fields(“*”).Do()
  10. Update͢Δͱ͖ͷtips • List APIܦ༝ͰFile ObjectΛऔಘ • ಛఆͷFieldΛߋ৽ͯ͠Update͢Δͱ list, err :=

    service.Files.List().Context(ctx).Do() if err != nil { return err }
 file := list[0]
 file.Name = “dummy”
 _, err := service.Files.Update(file.Id, file).Context(ctx).Do() if err != nil { return errors.Wrap(err, "error updated") }
 
 > The resource body includes fields which are not directly writable.
  11. Update͢Δͱ͖ͷtips • APIͰ͸ߋ৽Ͱ͖ͳ͍Field΋ߋ৽͕͔͔Δ • ߋ৽͍ͨ͠FiledΛࢦఆ list, err := service.Files.List().Context(ctx).Do() if

    err != nil { return err }
 
 file := list[0]
 _, err := service.Files.Update(file.Id, &drive.File{ Name: “dummy”, }).Context(ctx).Do() 
 if err != nil { return errors.Wrap(err, "error updated") }
  12. Update͢Δͱ͖ͷtips • ϑΝΠϧͷத਎Λߋ৽ • Media(io.Reader, Content-Type) Λ࢖༻ list, err :=

    service.Files.List().Context(ctx).Do() if err != nil { return err }
 
 file := list[0]
 content := &bytes.Buffer{} content.WriteString(“hello”)
 updatedFile, err := service.Files.Update(file.Id, &drive.File{}). Media(content, googleapi.ContentType(file.MimeType)).Context(ctx).Do()
  13. ࠶ؼతʹϑΝΠϧΛऔಘͰ͖ͳ͍ • ৄ͘͠͸ Google Drive API v3 Ͱݕࡧ • sample:

    Konboi/go-google-drive-example
 list, err := service.Files.List().Q(fmt.Sprintf("'%s' in parents", driveID)).Context(ctx).Do() if err != nil { return errors.Wrap(err, "list error") }
 
 typeFolder := "application/vnd.google-apps.folder" for _, f := range list.Files { pp.Printf("file id:%s\nname%s\ntype:%v\n", f.Id, f.Name, f.MimeType) if f.MimeType == typeFolder { err := ListRecursive(ctx, service, f.Id) if err != nil { return err } } }
  14. GoͰେྔͷϑΝΠϧΛੜ੒͢Δ • ̍ຕ 0.1sec • 10ຕ 1sec • 1000ຕ 100sec

    • 1ສຕ 1000sec (2.5h) • 100ສຕ 100000sec (27h) • 1000ສຕ 1000000sec (270h) • …
  15. GoͰେྔͷϑΝΠϧΛੜ੒͢Δ • ̍ຕ 100kb • 10ຕ 1000kb • 1000ຕ 100MB

    • 1ສຕ 1GB • 100ສຕ 100GB • 1000ສຕ 1TB • …
  16. GoͰେྔͷϑΝΠϧΛੜ੒͢Δ func GenCode(w http.ResponseWriter, r *http.Request) { v := r.URL.Query()

    code := v.Get("code") img, err := genCode(code, baseImage) if err != nil { return } buf := &bytes.Buffer{} if err := png.Encode(buf, img); err != nil { return } if err := upload(r.Context(), buf, filename); err != nil { return } fmt.Fprintf(w, "OK") w.WriteHeader(http.StatusOK) }
  17. GoͰେྔͷϑΝΠϧΛੜ੒͢Δ $ zip -r function.zip function.go some.png vendor $ gcloud

    beta functions deploy \ --project=${PROJECT_ID} \ --region=${REGION} \ --memory=${MEMORY} \ --runtime=${RUNTIME} \ --service-account=${SERVICE_ACCOUNT} \ --source=gs://${UPLOAD_BUCKET_PATH}/${FUNCTION}.zip \ ${FUNCTION} \ --entry-point=${FUNCTION} \ —trigger-http
  18. GoͰେྔͷϑΝΠϧΛੜ੒͢Δ • func (w http.Writer, r *http.Request) Λ
 ΄Μͱͦͷ··ΞοϓϩʔυͰ͖Δ •

    ґଘpackage΋ҰॹʹzipʹؚΊΕ͹࢖༻Մೳ • assetϑΝΠϧͳͲ΋ಉࠝՄೳ • zipϑΝΠϧ͓Αͼղౚͨ͠ͱ͖ͷ༰ྔ੍ݶ༗