首页 > 编程语言 > 详细

使用Go语言优雅地统计单词个数

时间:2019-08-09 22:01:59      阅读:117      评论:0      收藏:0      [点我收藏+]
type WordCounter int func (c *WordCounter) Write(p []byte) (int, error) { scanner := bufio.NewScanner(bytes.NewReader(p)) // set the split function for the scanning operation scanner.Split(bufio.ScanWords) // Count the words count := 0 for scanner.Scan() { count++ } if err := scanner.Err(); err != nil { return -1, err } *c += WordCounter(count) return len(p), nil } func main() { input := "Spicy jalapeno pastrami ut ham turducken.\n Lorem sed ullamco, leberkas sint short loin strip steak ut shoulder shankle porchetta venison prosciutto turducken swine.\n Deserunt kevin frankfurter tongue aliqua incididunt tri-tip shank nostrud.\n" var c WordCounter _, err := fmt.Fprintf(&c, "%s", input) if nil != err { log.Fatal(err) } fmt.Println(c) // 32 }

使用Go语言优雅地统计单词个数

原文:https://blog.51cto.com/11317783/2428234

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!