Golang bufio包使用范例

前端之家收集整理的这篇文章主要介绍了Golang bufio包使用范例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

bufio包实现了有缓冲的I/O。它包装一个io.Reader或io.Writer接口对象,创建另一个也实现了该接口,且同时还提供了缓冲和一些文本I/O的帮助函数的对象。


范例1:统计输入行数据出现的次数

package main


import (
"bufio"
"fmt"
"os"
)

func main() {

counts := make(map[string]int)
input := bufio.NewScanner(os.Stdin) // 命令行控制台输入
for input.Scan() { // 遇到 \n 或者\r\n循环一次,每输入一行内容必须回车,退出循环按组合键 Ctrl +D
counts[input.Text()]++
}
// NOTE: ignoring potential errors from input.Err()
for line,n := range counts {
if n > 1 {
fmt.Printf("%d\t%s\n",n,line)
}
}
fmt.Println(counts)

}

scanner对象从程序的标准输入中读取内容。对input.Scanner的每一次调用都会调入一个新行,并且会
自动将其行末的换行符去掉;结果用input.Text()得到。Scan方法在读到了新行的时候会返回true,而
在没有新行被读入时,会返回false。

[root@contoso ~]# cd $GOPATH && go install contoso.org/book && cd ~

[root@contoso ~]# book
ABC
DEF
GHI
GHI
ABC
ABC
ABC
JKL
JKL

4 ABC
2 GHI
2 JKL
map[GHI:2 JKL:2 ABC:4 DEF:1]

[root@contoso ~]#

范例2:使用日志直接打印输出控制台输入的内容

package main

import (
"bufio"
"log"
"os"
)

func main() {
s := bufio.NewScanner(os.Stdin)
for s.Scan() { // 当扫描因为抵达输入流结尾或者遇到错误而停止时,本方法会返回false。
log.Println("line",s.Text())
}
}

[root@contoso ~]# cd $GOPATH && go install contoso.org/book && cd ~
[root@contoso ~]# book
hi,welcome to china !
2017/07/02 22:24:58 line hi,welcome to china !
i'm glad to see you again.
2017/07/02 22:25:29 line i'm glad to see you again.
[root@contoso ~]#


范例3:

package main

import (
"bufio"
"fmt"
"strings"
)

func main() {
r := strings.NewReader("ABC\nDEF\r\nGHI\nJKL") // r是一个 *Read指针
bs := bufio.NewScanner(r)
for bs.Scan() { // 当扫描因为抵达输入流结尾或者遇到错误而停止时,本方法会返回false。
fmt.Printf("%d %v\n",bs.Bytes(),bs.Text())
}
}

[root@contoso ~]# cd $GOPATH && go install contoso.org/book && cd ~
[root@contoso ~]# book ## 直接输出全部内容

[65 66 67] ABC
[68 69 70] DEF
[71 72 73] GHI
[74 75 76] JKL

[root@contoso ~]#


范例4:

package main

import (
"bufio"
"fmt"
"os"
)

func main() {
w := bufio.NewWriter(os.Stdout)
fmt.Fprint(w,"Hello,")
fmt.Fprint(w,"world!\n")
w.Flush() // Don't forget to flush!
}

[root@contoso ~]# cd $GOPATH && go install contoso.org/book && cd ~
[root@contoso ~]# book
Hello,world!

[root@contoso ~]#

范例5:Example (Custom) http://studygolang.com/pkgdoc

package main

import (
"bufio"
"fmt"
"strconv"
"strings"
)

func main() {
// An artificial input source.
const input = "1234 5678 1234567901234567890"
scanner := bufio.NewScanner(strings.NewReader(input)) // Create a custom split function by wrapping the existing ScanWords function.
split := func(data []byte,atEOF bool) (advance int,token []byte,err error) {
advance,token,err = bufio.ScanWords(data,atEOF)
if err == nil && token != nil {
_,err = strconv.ParseInt(string(token),10,32)
}
return
}
// Set the split function for the scanning operation.
scanner.Split(split) // Validate the input
for scanner.Scan() {
fmt.Printf("%s\n",scanner.Text())
}
if err := scanner.Err(); err != nil {
fmt.Printf("Invalid input: %s",err)
}
}

调试控制台打印输出如下信息:

2017/07/02 23:16:05 server.go:73: Using API v1
2017/07/02 23:16:05 debugger.go:97: launching process with args: [/root/code/go/src/contoso.org/book/debug 141 13 105 17 208]
API server listening at: 127.0.0.1:2345
2017/07/02 23:16:06 debugger.go:505: continuing
1234
5678
Invalid input: strconv.ParseInt: parsing "1234567901234567890": value out of range


范例6:Example (Lines) http://studygolang.com/pkgdoc

package main

import (
"bufio"
"fmt"
"os"
)

func main() {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
fmt.Println(scanner.Text()) // Println will add back the final '\n'
}
if err := scanner.Err(); err != nil {
fmt.Fprintln(os.Stderr,"reading standard input:",err)
}
}

[root@contoso ~]# cd $GOPATH && go install contoso.org/book && cd ~
[root@contoso ~]# book
this is a testing line1

this is a testing line1
this is a testing line2 ## 粗体是手动输入的行,例如定向输入到文件 book >> data.txt
this is a testing line2
this is a tesing line3
this is a tesing line3
[root@contoso ~]#

范例7:Example (Words)http://studygolang.com/pkgdoc

package main

import (
"bufio"
"fmt"
"os"
"strings"
)

func main() {
// An artificial input source.
const input = "Now is the winter of our discontent,\nMade glorIoUs summer by this sun of York.\n"
scanner := bufio.NewScanner(strings.NewReader(input))
// 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 {
fmt.Fprintln(os.Stderr,"reading input:",err)
}
fmt.Printf("%d\n",count)
}

[root@contoso ~]# cd $GOPATH && go install contoso.org/book && cd ~
[root@contoso ~]# book
15
[root@contoso ~]#

范例8:两个Scanner分别从不同的文件中按行取内容,用嵌套循环生产一个n*m的列表

package main

import (
"bufio"
"fmt"
"os"
)

func main() {
userFile := "user.txt"
passFile := "pass.txt"
userf,usererr := os.OpenFile(userFile,os.O_RDONLY,0660)
passf,passerr := os.OpenFile(passFile,0660)
defer userf.Close()
defer passf.Close()
if usererr != nil || passerr != nil {
fmt.Printf("OpenFile Error!")
return
}
i := 0
userScanner := bufio.NewScanner(userf)
passScanner := bufio.NewScanner(passf)
tokens := map[int]string{}
for userScanner.Scan() {
user := userScanner.Text()
for passScanner.Scan() {
pass := passScanner.Text()
tokens[i] = user + ":" + pass
i++
}
passf.Seek(0,0)
passScanner = bufio.NewScanner(passf)
}
fmt.Println(len(tokens))
for _,token := range tokens {
fmt.Println(token)
}
}

[root@contoso ~]# tree $GOPATH/src/contoso.org -L 3 ##查看项目目录结构
/root/code/go/src/contoso.org
├── book
├── debug
├── main.go
├── pass.txt
└── user.txt

[root@contoso ~]# more $GOPATH/src/contoso.org/book/user.txt
user1
user2
user3
user4
user5
user6
user7
user8
user9
user10
user11
user12
user13
user14
user15
user16
user17
user18
user19
user20
[root@contoso ~]# more $GOPATH/src/contoso.org/book/pass.txt
password1
password2
password3
password4
password5
password6
password7
password8
password9
password10
password11
password12
password13
password14
password15
password16
password17
password18
password19
password20
[root@contoso ~]#

[root@contoso ~]# cd $GOPATH/src/contoso.org/book
[root@contoso book]# go run main.go ## 临时性非全局执行程序 400 user1:password8 user3:password3 user3:password7 user4:password12 user5:password3 user6:password15 user7:password4 user1:password6 user20:password11 user11:password18 user19:password14 user20:password4 user2:password4 user6:password7 user9:password4 user12:password7 user14:password13 user15:password15 user16:password15 user17:password1 user2:password11 user17:password10 user6:password16 user8:password6 user9:password1 user3:password19 user4:password1 user9:password6 user10:password4 user11:password14 user11:password19 user13:password6 user14:password1 user3:password1 user19:password11 user20:password12 user16:password20 user4:password19 user6:password3 user11:password7 user12:password6 user14:password8 user14:password19 user16:password19 user3:password2 user20:password16 user17:password14 user2:password18 user7:password6 user8:password4 user12:password13 user14:password12 user16:password17 user18:password12 user1:password9 user19:password8 user18:password17 user7:password5 user7:password13 user11:password6 user1:password20 user3:password5 user3:password18 user4:password2 user8:password17 user10:password8 user15:password9 user16:password8 user1:password1 user18:password3 user17:password17 user5:password9 user7:password14 user9:password11 user12:password16 user16:password7 user18:password20 user2:password10 user3:password12 user8:password19 user16:password6 user17:password12 user18:password4 user1:password15 user5:password5 user9:password10 user12:password4 user12:password18 user13:password10 user14:password9 user18:password5 user3:password9 user19:password16 user11:password16 user12:password3 user19:password5 user1:password19 user5:password6 user9:password17 user15:password18 user16:password1 user18:password18 user2:password9 user2:password14 user4:password11 user6:password13 user8:password10 user10:password13 user11:password4 user20:password5 user2:password12 user20:password6 user11:password10 user12:password12 user12:password17 user14:password2 user14:password4 user15:password13 user17:password16 user6:password10 user20:password18 user6:password20 user4:password8 user7:password12 user11:password17 user12:password14 user12:password20 user18:password10 user18:password15 user3:password15 user1:password16 user2:password3 user2:password7 user2:password13 user5:password10 user1:password7 user4:password10 user4:password16 user11:password1 user11:password20 user16:password10 user3:password13 user10:password3 user19:password10 user20:password2 user4:password6 user13:password2 user14:password6 user15:password4 user20:password15 user12:password2 user8:password8 user11:password15 user17:password4 user7:password10 user6:password2 user8:password1 user9:password3 user2:password15 user5:password11 user13:password7 user1:password10 user5:password16 user10:password15 user11:password12 user15:password16 user18:password9 user19:password3 user3:password6 user12:password1 user13:password11 user15:password14 user17:password20 user19:password7 user6:password12 user13:password14 user14:password20 user6:password9 user3:password17 user7:password15 user12:password19 user15:password5 user16:password11 user3:password11 user6:password4 user8:password14 user16:password5 user18:password2 user19:password2 user19:password13 user20:password1 user5:password4 user5:password8 user7:password8 user8:password12 user20:password9 user20:password17 user3:password10 user10:password5 user10:password16 user12:password10 user13:password18 user16:password18 user18:password7 user8:password3 user4:password13 user8:password5 user8:password11 user10:password11 user13:password4 user14:password17 user20:password8 user1:password18 user7:password16 user7:password17 user9:password5 user6:password8 user11:password9 user13:password17 user16:password2 user18:password13 user18:password19 user3:password16 user7:password9 user15:password11 user19:password6 user2:password16 user3:password14 user5:password19 user10:password1 user14:password14 user19:password12 user20:password14 user1:password14 user13:password3 user15:password2 user17:password3 user19:password19 user9:password19 user9:password2 user15:password17 user17:password11 user5:password13 user5:password17 user9:password8 user10:password18 user17:password5 user3:password4 user4:password20 user5:password7 user7:password3 user10:password10 user13:password20 user16:password4 user18:password11 user4:password9 user20:password10 user19:password9 user8:password16 user12:password9 user13:password12 user14:password5 user7:password2 user1:password13 user10:password14 user13:password1 user14:password18 user15:password8 user19:password17 user1:password3 user6:password14 user8:password7 user9:password12 user13:password16 user2:password19 user2:password2 user5:password15 user6:password18 user8:password18 user11:password8 user13:password8 user19:password4 user1:password11 user20:password19 user19:password15 user8:password2 user20:password3 user2:password8 user1:password5 user4:password17 user7:password19 user9:password20 user10:password9 user11:password13 user15:password19 user1:password2 user16:password14 user10:password12 user18:password14 user5:password12 user2:password20 user4:password3 user9:password16 user17:password6 user17:password9 user2:password6 user11:password11 user14:password10 user16:password16 user17:password18 user20:password13 user10:password20 user3:password8 user9:password18 user17:password7 user17:password8 user17:password15 user19:password1 user1:password12 user9:password9 user12:password11 user13:password5 user13:password13 user8:password20 user6:password17 user8:password9 user8:password13 user9:password14 user18:password16 user6:password6 user4:password7 user6:password5 user9:password13 user10:password2 user12:password15 user16:password9 user1:password4 user7:password20 user11:password2 user13:password15 user16:password12 user18:password6 user5:password1 user5:password2 user5:password20 user7:password1 user7:password11 user7:password18 user12:password8 user14:password11 user2:password1 user16:password13 user6:password19 user17:password13 user20:password20 user2:password5 user4:password5 user10:password19 user15:password7 user15:password10 user4:password4 user2:password17 user5:password14 user9:password7 user13:password19 user14:password3 user15:password6 user15:password12 user1:password17 user18:password1 user17:password19 user10:password6 user11:password3 user12:password5 user13:password9 user14:password15 user14:password16 user19:password18 user7:password7 user5:password18 user14:password7 user15:password1 user18:password8 user4:password15 user10:password17 user16:password3 user19:password20 user10:password7 user8:password15 user9:password15 user11:password5 user15:password20 user4:password14 user4:password18 user6:password1 user6:password11 user15:password3 user17:password2 user20:password7 user3:password20 [root@contoso book]#

猜你在找的Go相关文章