golang

golang file scanner

kimbs0301 2024. 7. 5. 17:21

fs.Scan() 메서드 사용으로 텍스트 파일 전체 읽기

 

go.mod

더보기

module example.com/fs/main

go 1.20

 

main.go

// Package main
package main // import "example.com/fs/main"

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

// main
func main() {
	f, err := os.Open("file1.txt")
	if err != nil {
		fmt.Println(err)
		return
	}

	fs := bufio.NewScanner(f)
	fs.Split(bufio.ScanLines)

	for fs.Scan() {
		fmt.Println(fs.Text())
	}

	f.Close()
}