package main
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"os"
"testing"
)
const fileName = "README.md"
//按照处理的字节数读取
func TestReadFilr(t *testing.T) {
file, err := os.Open(fileName)
if err != nil {
t.Error(err)
}
buffer := make([]byte, 1024)
for {
if a, b := file.Read(buffer); a != 0 && b != io.EOF {
fmt.Println(b)
} else {
return
}
}
}
// 一次性读取
func TestReadFile2(t *testing.T) {
if d, err := ioutil.ReadFile(fileName); err == nil {
str := string(d)
fmt.Println(str)
} else {
t.Error(err)
}
}
//TestReadFileLBL line by line
func TestReadFileLBL(t *testing.T) {
if file, err := os.Open(fileName); err == nil {
buffer := bufio.NewScanner(file)
position := 0
for buffer.Scan() {
desc := fmt.Sprintf("%d >> %s", position, buffer.Text())
fmt.Println(desc)
}
}
}
以上所述就是小编给大家介绍的《go--读取文件的方式》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- 概述 Go 中读取文件的方式
- 【TensorFlow2.0】数据读取与使用方式
- Spark Streaming 场景应用 — Kafka 数据读取方式
- Spring Boot 2 实战:常用读取配置的方式
- Spring Boot读取配置文件的几种方式
- C# 读取和生成 Excel 的简单方式-ClosedXML
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Agile Web Development with Rails, Third Edition
Sam Ruby、Dave Thomas、David Heinemeier Hansson / Pragmatic Bookshelf / 2009-03-17 / USD 43.95
Rails just keeps on changing. Rails 2, released in 2008, brings hundreds of improvements, including new support for RESTful applications, new generator options, and so on. And, as importantly, we’ve a......一起来看看 《Agile Web Development with Rails, Third Edition》 这本书的介绍吧!