9

[Golang] Read Lines From File or String

 2 years ago
source link: http://siongui.github.io/2016/04/06/go-readlines-from-file-or-string/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Readlines From String

Run Code on Go Playground

import (
      "bufio"
      "strings"
)

func StringToLines(s string) (lines []string, err error) {
      scanner := bufio.NewScanner(strings.NewReader(s))
      for scanner.Scan() {
              lines = append(lines, scanner.Text())
      }
      err = scanner.Err()
      return
}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK