1、确保已经安装go语言
2、代码实现
A.这里我采用了开源的API,https://github.com/denghongcai/pullword,简单的实现了功能
B.代码:
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 } |
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
|
package main import ( "fmt" "os" "pullword" "io/ioutil" ) func main() { result_str := "" result_str = readFile( "/home/tian/Desktop/test.txt" ) req := pullword.NewRequest(result_str, 1 , true ) result, err := req.Do() if err != nil { panic(err) } fmt.Printf( "%q" , result) } func readFile(path string) string { fi,err := os.Open(path) if err != nil{panic(err)} defer fi.Close() fd,err := ioutil.ReadAll(fi) return string(fd) } |
1
2
|
test.txt 你的姿势水平还远远不够 |
C.运行结果:
1
|
[ "姿势:1" "水平:1" "远远:1" "不够:1" ] |
有疑问加站长微信联系(非本文作者)