-
Notifications
You must be signed in to change notification settings - Fork 0
/
playground.go
64 lines (56 loc) · 1.06 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
60
61
62
63
64
package eval
import (
"encoding/json"
"io/ioutil"
"net/http"
"strings"
)
const helloworld = `package main
import (
"fmt"
)
func main() {
fmt.Println("Hello, playground")
}`
// Events for JSON
type Events struct {
Message string
Kind string
Delay int
}
// GolangResult for JSON
type GolangResult struct {
Errors string
Events []Events
Status int
IsTest bool
TestsFailed int
}
// GoCode run code and response
func GoCode(code string) (*GolangResult, error) {
var r http.Request
r.ParseForm()
r.Form.Add("version", "2")
if code == "" {
code = helloworld
}
r.Form.Add("body", code)
body := strings.NewReader(r.Form.Encode())
resp, err := http.Post("https://play.golang.org/compile", "application/x-www-form-urlencoded", body)
if err != nil {
// fmt.Println(err)
return nil, err
}
content, err := ioutil.ReadAll(resp.Body)
if err != nil {
// fmt.Println(err)
return nil, err
}
var res GolangResult
err = json.Unmarshal(content, &res)
if err != nil {
// fmt.Println(err)
return nil, err
}
return &res, nil
}