Go: split byte.Buffer by newline

huangapple go评论66阅读模式
英文:

Go: split byte.Buffer by newline

问题

你好!以下是你要翻译的内容:

对Go语言还比较新,遇到了一个问题:

var metrics bytes.Buffer

metrics.WriteString("foo")
metrics.WriteString("\n")

metrics.WriteString("bar")
metrics.WriteString("\n")

现在我想遍历这个metrics并按换行符进行分割。我尝试了以下代码:

for _, m := range strings.Split(metrics.String(), "\n") {
    log.Printf("metric: %s", m)
}

但是我得到了以下错误信息:

./relay.go:71: m := strings.Split(metrics.String(), "\n") used as value

希望对你有帮助!如果你有任何其他问题,请随时提问。

英文:

Pretty new to Go and running into a problem like:

var metrics bytes.Buffer

metrics.WriteString("foo")
metrics.WriteString("\n")

metrics.WriteString("bar")
metrics.WriteString("\n")

Now I want to cycle through that metrics and split by newline. I tried

for m := strings.Split(metrics.String(), "\n") {
     log.Printf("metric: %s", m)
}

but I get the following

./relay.go:71: m := strings.Split(metrics.String(), "\n") used as value

答案1

得分: 14

你可以使用bufio.Scanner来实现这个功能。具体的使用方法可以参考http://golang.org/pkg/bufio/#Scanner。

代码示例如下:

import (
	"bufio"
	"bytes"
	"log"
)

func main() {
	var metrics bytes.Buffer

	metrics.WriteString("foo")
	metrics.WriteString("\n")

	metrics.WriteString("bar")
	metrics.WriteString("\n")

	scanner := bufio.NewScanner(&metrics)
	for scanner.Scan() {
		log.Printf("metric: %s", scanner.Text())
	}

	if err := scanner.Err(); err != nil {
		log.Fatal(err)
	}
}

你可以在这里找到一个完整的示例:http://play.golang.org/p/xrFEGF3h5P

英文:

You can do this with a bufio.Scanner
Godoc at http://golang.org/pkg/bufio/#Scanner

Something like this:

var metrics bytes.Buffer

metrics.WriteString("foo")
metrics.WriteString("\n")

metrics.WriteString("bar")
metrics.WriteString("\n")

scanner := bufio.NewScanner(&metrics)
for scanner.Scan() {
	log.Printf("metric: %s", scanner.Text())
}

if err := scanner.Err(); err != nil {
	log.Fatal(err)
}

A full example here: http://play.golang.org/p/xrFEGF3h5P

答案2

得分: 8

考虑到strings.Split()返回一个数组,使用range会更容易一些:

m := strings.Split(metrics.String(), "\n")
for _, m := range strings.Split(metrics.String(), "\n") {
    log.Printf("metric: %s", m)
}

注意,要从字符串中读取行,可以考虑使用bufio.ReadLine()或更好的bufio.Scanner

const input = "Now is the winter of our discontent,\nMade glorious summer by this sun of York.\n"
scanner := bufio.NewScanner(strings.NewReader(input))

更多信息请参考"Scanner terminating early"。

英文:

Considering that strings.Split() returns an array, it would be easier to use range

m := strings.Split(metrics.String(), "\n")
for _, m := range strings.Split(metrics.String(), "\n") {
    log.Printf("metric: %s", m)
}

Note, to read lines from a string, you can consider "go readline -> string":

bufio.ReadLine() or better: bufio.Scanner

As in:

const input = "Now is the winter of our discontent,\nMade glorious summer by this sun of York.\n"
scanner := bufio.NewScanner(strings.NewReader(input))

See more at "Scanner terminating early".

huangapple
  • 本文由 发表于 2014年11月6日 04:11:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/26766195.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定