[Go] OS - bufio readfile

package main

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

type name struct {
	fname []byte
	lname []byte
}

func main() {
	fmt.Print("File path ")

	var filename string
	fmt.Scanln(&filename)

	file, _ := os.Open(filename)
	defer file.Close()

	names := make([]name, 0)

	s := bufio.NewScanner(file)
	for s.Scan() {
		s := strings.Split(s.Text(), " ")
		n := name{fname: []byte(s[0]), lname: []byte(s[1])}

		names = append(names, n)
	}

	for _, n := range names {
		fmt.Printf("first name: %s, last name: %s\n", n.fname, n.lname)
	}

}

 

posted @ 2022-09-13 20:39  Zhentiw  阅读(12)  评论(0编辑  收藏  举报