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

The Go Playgoroundで 外部パッケージ使いたい

The Go Playgoroundで 外部パッケージ使いたい

Go(Un)Conference(Goあんこ)LT大会 4kg https://gounconference.connpass.com/event/99487/ の発表資料です。

9ec2ceee3f782e64fd67fa7d82b6c1b4?s=128

Hazumi Ichijo

October 19, 2018
Tweet

More Decks by Hazumi Ichijo

Other Decks in Programming

Transcript

  1. ©2018 Wantedly, Inc. The Go PlaygoroundͰ ֎෦ύοέʔδ࢖͍͍ͨ Go(Un)Conference 4kg 19.Oct.2018

    - Hazumi Ichijo
  2. ©2018 Wantedly, Inc. ࣗݾ঺հ Ұᑍ୺੅!IB[VNJSS!SFSPTU 8FC"QQMJDBUJPO&OHJOFFS (P 3BJMT 3FBDUʜ ۴࿏ߴઐஜ೾େֶ8BOUFEMZ

  3. ©2018 Wantedly, Inc.  1MBZHSPVOE  JSC  SBJMTDPOTPMF ࢼ͠ʹίʔυΛಈ͔͍ͨ͠ͱ͖

  4. ©2018 Wantedly, Inc.  1MBZHSPVOE  JSC  SBJMTDPOTPMF ࢼ͠ʹίʔυΛಈ͔͍ͨ͠ͱ͖

    ֎෦ύοέʔδ࢖͑ͳ͍ɾɾɾ
  5. ©2018 Wantedly, Inc. package main import ( "fmt" "gopkg.in/guregu/null.v3" )

    func main() { fmt.Println(null.StringFrom("PlayGround")) }
  6. ©2018 Wantedly, Inc. HPHFUΛ࣮ߦ͢Δ͚ͩͰ ύοέʔδͷґଘղܾͯ͘͠Εͨ͸ͣɾɾɾʁ

  7. ©2018 Wantedly, Inc. func compileAndRun(req *request) (*response, error) { //

    TODO(andybons): Add semaphore to limit number of running programs at once. tmpDir, err := ioutil.TempDir("", "sandbox") if err != nil { return nil, fmt.Errorf("error creating temp directory: %v", err) } defer os.RemoveAll(tmpDir) src := []byte(req.Body) in := filepath.Join(tmpDir, "main.go") if err := ioutil.WriteFile(in, src, 0400); err != nil { return nil, fmt.Errorf("error creating temp file %q: %v", in, err) } https://github.com/golang/playground/blob/master/sandbox.go
  8. ©2018 Wantedly, Inc. func compileAndRun(req *request) (*response, error) { //

    TODO(andybons): Add semaphore to limit number of running programs at once. tmpDir, err := ioutil.TempDir("", "sandbox") if err != nil { return nil, fmt.Errorf("error creating temp directory: %v", err) } defer os.RemoveAll(tmpDir) src := []byte(req.Body) in := filepath.Join(tmpDir, "main.go") if err := ioutil.WriteFile(in, src, 0400); err != nil { return nil, fmt.Errorf("error creating temp file %q: %v", in, err) } https://github.com/golang/playground/blob/master/sandbox.go
  9. ©2018 Wantedly, Inc. ݁ہϑΝΠϧʹॻ͖ग़ͯ͠Ϗϧυ͍ͯ͠Δ͚ͩͳͷͰ  ॻ͖ग़͠ઌΛ(01"5)ҎԼʹ͢Δ  HPHFUΛ࣮ߦ͢Δ ͜Ε͚ͩͰ͍͚Δʁʢ͍͚ΔΘ͚͕ͳͦ͞͏ʣ

  10. ©2018 Wantedly, Inc. https://github.com/rerost/playground/compare/e07747d8d2cee6639b14ba8021d453a6b8dd9edc...master

  11. ©2018 Wantedly, Inc.

  12. ©2018 Wantedly, Inc. ಈ͍ͯ͠·ͬͨɾɾɾ

  13. ©2018 Wantedly, Inc. https://play-dot-k8s-test-219404.appspot.com/ @hazumirr

  14. ©2018 Wantedly, Inc. ͳͥ͜ͷํ๏Ͱ্ख͍͔ͬͨ͘

  15. ©2018 Wantedly, Inc. ͳͥ͏·͘ߦͬͨͷ͔  4BOECPY͕ίʔυͷ࣮ߦ࣌ͷΈ༗ޮʹͳ͍ͬͯͨ  ػೳ͕γϯϓϧͩͬͨ

  16. ©2018 Wantedly, Inc. cmd.Env = []string{"GOOS=nacl", "GOARCH=amd64p32", "GOPATH=" + os.Getenv("GOPATH")}

    if out, err := cmd.CombinedOutput(); err != nil { if _, ok := err.(*exec.ExitError); ok { // Return compile errors to the user. // Rewrite compiler errors to refer to progName // instead of '/tmp/sandbox1234/main.go'. errs := strings.Replace(string(out), in, progName, -1) // "go build", invoked with a file name, puts this odd // message before any compile errors; strip it. errs = strings.Replace(errs, "# command-line-arguments\n", "", 1) return &response{Errors: errs}, nil } return nil, fmt.Errorf("error building go source: %v", err) } ctx, cancel := context.WithTimeout(context.Background(), maxRunTime) defer cancel() cmd = exec.CommandContext(ctx, "sel_ldr_x86_64", "-l", "/dev/null", "-S", "-e", exe, testParam) https://github.com/golang/playground/blob/master/sandbox.go#L320-L339
  17. ©2018 Wantedly, Inc. cmd.Env = []string{"GOOS=nacl", "GOARCH=amd64p32", "GOPATH=" + os.Getenv("GOPATH")}

    if out, err := cmd.CombinedOutput(); err != nil { if _, ok := err.(*exec.ExitError); ok { // Return compile errors to the user. // Rewrite compiler errors to refer to progName // instead of '/tmp/sandbox1234/main.go'. errs := strings.Replace(string(out), in, progName, -1) // "go build", invoked with a file name, puts this odd // message before any compile errors; strip it. errs = strings.Replace(errs, "# command-line-arguments\n", "", 1) return &response{Errors: errs}, nil } return nil, fmt.Errorf("error building go source: %v", err) } ctx, cancel := context.WithTimeout(context.Background(), maxRunTime) defer cancel() cmd = exec.CommandContext(ctx, "sel_ldr_x86_64", "-l", "/dev/null", "-S", "-e", exe, testParam) https://github.com/golang/playground/blob/master/sandbox.go#L320-L339
  18. ©2018 Wantedly, Inc. NaClͱ͸  αϯυϘοΫεΛ࣮ݱ͢ΔͨΊͷ΋ͷɹ  $ISPNFͷ΢ΣϒετΞͰ഑෍͞ΕΔ
 ΞϓϦέʔγϣϯΛ࣮ߦ͢Δͷʹ࢖ΘΕΔ΋ͷ 

    /B$M༻ʹϏϧυ͞ΕͨόΠφϦ͕ඞཁ<ཁग़య> https://ja.wikipedia.org/wiki/Google_Native_Client
  19. ©2018 Wantedly, Inc. ͳͥ͏·͍ͬͨ͘ͷ͔ /B$M  ίʔυͷ࣮ߦͷࡍͷΈ༗ޮͩͬͨ  /B$MͰ4BOECPY͕࣮ݱ͞Ε͍ͯͨ 

    (P͕/B$M༻ͷόΠφϦΛ࡞ΕΔʢ(004OBDMʣɹ
  20. ©2018 Wantedly, Inc. ͳͥ͏·͍ͬͨ͘ͷ͔

  21. ©2018 Wantedly, Inc. https://play-dot-k8s-test-219404.appspot.com/ ੬ऑੑ΍ෆ۩߹ͳͲΛݟ͚ͭͨΒڭ͍͑ͯͩ͘͞