1、确保已经安装go语言
2、代码实现
A.这里我采用了开源的API,https://github.com/denghongcai/pullword,简单的实现了功能
B.代码:
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 }
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) }
test.txt 你的姿势水平还远远不够
C.运行结果:
["姿势:1" "水平:1" "远远:1" "不够:1"]
有疑问加站长微信联系(非本文作者)