-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
58 lines (41 loc) · 1.02 KB
/
main.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
package wolframgo
import (
"encoding/json"
"io/ioutil"
"net/http"
"strings"
)
var AppID string
func SetAppId(id string) {
AppID = id
}
func GetSimpleResult(question string) (string, error) {
link := "http://api.wolframalpha.com/v2/result?appid=" + AppID + "&output=json&input=" + strings.ReplaceAll(question, " ", "+")
res, err := http.Get(link)
defer res.Body.Close()
if err != nil {
return "", err
}
result, err := ioutil.ReadAll(res.Body)
if err != nil {
return "", err
}
return string(result), nil
}
func GetComplexResult(question string) (*ComplexResult, error) {
link := "http://api.wolframalpha.com/v2/query?appid=" + AppID + "&includepodid=Result&output=json&input=" + strings.ReplaceAll(question, " ", "+")
res, err := http.Get(link)
defer res.Body.Close()
if err != nil {
return nil, err
}
result, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
a := baseResult{}
if err := json.Unmarshal(result, &a); err != nil {
return nil, err
}
return &a.Result, nil
}