-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
51 lines (39 loc) · 825 Bytes
/
main.go
File metadata and controls
51 lines (39 loc) · 825 Bytes
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
package main
import (
"encoding/json"
"fmt"
"net/http"
"astuart.co/goq"
)
type Wiki struct {
Title string `goquery:"h1"`
Content []string `goquery:"div.mw-parser-output p"`
}
func main() {
kw := "Logistic Regression"
w := wiki(kw)
if len(w.Content) >= 3 {
w.Content = w.Content[0:3]
}
fmt.Println(PrettyPrint(w))
}
// PrettyPrint to print struct in a readable way
func PrettyPrint(i interface{}) string {
s, _ := json.MarshalIndent(i, "", "\t")
return string(s)
}
func wiki(keyword string) Wiki {
// construct wiki links
link := fmt.Sprintf("https://en.wikipedia.org/wiki/%s", keyword)
res, err := http.Get(link)
if err != nil {
fmt.Println(err)
}
defer res.Body.Close()
var wiki Wiki
err = goq.NewDecoder(res.Body).Decode(&wiki)
if err != nil {
fmt.Println(err)
}
return wiki
}