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/ の発表資料です。

Hazumi Ichijo

October 19, 2018
Tweet

More Decks by Hazumi Ichijo

Other Decks in Programming

Transcript

  1. ©2018 Wantedly, Inc. package main import ( "fmt" "gopkg.in/guregu/null.v3" )

    func main() { fmt.Println(null.StringFrom("PlayGround")) }
  2. ©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
  3. ©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
  4. ©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
  5. ©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