본문 바로가기
Go/Packages

[Go] Package bufio

by llHoYall 2020. 10. 31.

Package bufio implements buffered I/O.

Methods

type Scanner

더보기

NewScanner

NewScanner returns a new Scanner to read from r.

func NewScanner (r io.Reader) *Scanner

Err

Err returns the first non-EOF error that was encountered by the Scanner.

func (s *Scanner) Err() error

Scan

Scan advances the Scanner to the next token, which will then be available through the Bytes or Text method.

It returns false when the scan stops, either by reaching the end of the input or an error.

After Scan returns false, the Err method will return any error that occurred during scanning, except that if it was io.EOF, Err will return nil.

func (s *Scanner) Scan() bool

Text

Text returns the most recent token generated by a call to Scan as a newly allocated string holding its bytes.

func (s *Scanner) Text() string

Usages

package main

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

func main() {
  file, err := os.Open("README.md")
  if err != nil {
    log.Fatal(err)
  }
  
  scanner := bufio.NewScanner(file)
  for scanner.Scan() {
    fmt.Println(scanner.Text())
  }

  err = file.Close()
  if err != nil {
    log.Fatal(err)
  }
  
  if scanner.Err() != nil {
    log.Fatal(scanner.Err())
  }
}

'Go > Packages' 카테고리의 다른 글

[Go] Package testing  (0) 2020.11.30
[Go] Package log  (0) 2020.11.30
[Go] Package errors  (0) 2020.10.29
[Go] Package math  (0) 2020.10.27
[Go] Package strconv  (0) 2020.10.26

댓글