-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpullword.go
56 lines (51 loc) · 1004 Bytes
/
pullword.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
package pullword
import (
"bufio"
"fmt"
"net"
"strings"
)
type request struct {
source string
param1 float32
param2 uint
}
func NewRequest(source string, threshold float32, debug bool) request {
var param2 uint
if debug {
param2 = 1
} else {
param2 = 0
}
return request{
source: source,
param1: threshold,
param2: param2,
}
}
func (req request) Do() ([]string, error) {
conn, err := net.Dial("tcp", "api.pullword.com:2015")
if err != nil {
return nil, err
}
writer := bufio.NewWriter(conn)
_, err = writer.WriteString(fmt.Sprintf("%s\t%1.2f\t%d]\r\n", req.source, req.param1, req.param2))
if err != nil {
return nil, err
}
writer.Flush()
if err != nil {
return nil, err
}
scanner := bufio.NewScanner(conn)
list := make([]string, 0)
for scanner.Scan() {
if scanner.Text() != "\r\n" && scanner.Text() != "" {
list = append(list, strings.Trim(scanner.Text(), "\r\n"))
}
}
if err := scanner.Err(); err != nil {
return nil, err
}
return list, nil
}