Go: split byte.Buffer by newline

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

Go: split byte.Buffer by newline

问题

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

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

  1. var metrics bytes.Buffer
  2. metrics.WriteString("foo")
  3. metrics.WriteString("\n")
  4. metrics.WriteString("bar")
  5. metrics.WriteString("\n")

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

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

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

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

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

英文:

Pretty new to Go and running into a problem like:

  1. var metrics bytes.Buffer
  2. metrics.WriteString("foo")
  3. metrics.WriteString("\n")
  4. metrics.WriteString("bar")
  5. metrics.WriteString("\n")

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

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

but I get the following

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

答案1

得分: 14

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

代码示例如下:

  1. import (
  2. "bufio"
  3. "bytes"
  4. "log"
  5. )
  6. func main() {
  7. var metrics bytes.Buffer
  8. metrics.WriteString("foo")
  9. metrics.WriteString("\n")
  10. metrics.WriteString("bar")
  11. metrics.WriteString("\n")
  12. scanner := bufio.NewScanner(&metrics)
  13. for scanner.Scan() {
  14. log.Printf("metric: %s", scanner.Text())
  15. }
  16. if err := scanner.Err(); err != nil {
  17. log.Fatal(err)
  18. }
  19. }

你可以在这里找到一个完整的示例: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:

  1. var metrics bytes.Buffer
  2. metrics.WriteString("foo")
  3. metrics.WriteString("\n")
  4. metrics.WriteString("bar")
  5. metrics.WriteString("\n")
  6. scanner := bufio.NewScanner(&metrics)
  7. for scanner.Scan() {
  8. log.Printf("metric: %s", scanner.Text())
  9. }
  10. if err := scanner.Err(); err != nil {
  11. log.Fatal(err)
  12. }

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

答案2

得分: 8

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

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

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

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

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

英文:

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

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

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

bufio.ReadLine() or better: bufio.Scanner

As in:

  1. const input = "Now is the winter of our discontent,\nMade glorious summer by this sun of York.\n"
  2. 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:

确定