-
Notifications
You must be signed in to change notification settings - Fork 4
/
playground.go
59 lines (54 loc) · 1.33 KB
/
playground.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"regexp"
)
var playgroundUrl = regexp.MustCompile(`https?\:\/\/play\.golang\.org\/p\/([a-zA-Z0-9_-]+)`)
type compileResult struct {
Errors string `json:"compile_errors"`
Output string `json:"output"`
}
func shrink(src string, size int) string {
runes := []rune(src)
if len(runes) > size {
return string(runes[:size]) + fmt.Sprintf("\nstring is too long (%d chars has been removed)", len(runes)-size)
}
return src
}
func playground(addr string) (string, error) {
res := ""
resp, err := http.Get(addr + ".go")
if err != nil {
return res, err
}
defer resp.Body.Close()
bytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return res, err
}
res = string(bytes)
resp, err = http.PostForm("https://play.golang.org/compile", map[string][]string{
"body": []string{res},
})
res = shrink(res, 1500)
if err != nil {
return res, err
}
defer resp.Body.Close()
compileRes := &compileResult{}
err = json.NewDecoder(resp.Body).Decode(compileRes)
if err != nil {
return res, err
}
res = fmt.Sprintf("```%s```", res)
if compileRes.Errors != "" {
res = fmt.Sprintf("\n Errors:\n```%s```", res, shrink(compileRes.Errors, 1000))
}
if compileRes.Output != "" {
res = fmt.Sprintf("%s\nResult:\n```%s```", res, shrink(compileRes.Output, 1000))
}
return res, nil
}