1 Star 0 Fork 0

c01dface/s-go-example

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
reading-files.go 2.22 KB
一键复制 编辑 原始数据 按行查看 历史
c01dface 提交于 2024-12-20 10:00 . add reading-files.go
package main
// Reading and writing files are basic tasks needed for many Go
// programs.
import (
"bufio"
"fmt"
"io"
"os"
)
// Reading files requires checking most calls for errors. This
// helper will streamline our error checks below
func check(e error) {
if e != nil {
panic(e)
}
}
func main() {
// Perhaps the most basic file reading task is slurping a file's
// entire contents into memory.
dat, err := os.ReadFile("/tmp/dat")
check(err)
fmt.Print(string(dat))
// You'll often want more control over how and what parts of a file
// are read. For these tasks, start by Opening a file to obtain an
// os.File value.
f, err := os.Open("/tmp/dat")
check(err)
// Read some bytes from the beginning of the file. Allow up to 5 to
// be read but also note how many actually were read.
b1 := make([]byte, 5)
n1, err := f.Read(b1)
check(err)
fmt.Printf("%d bytes: %s\n", n1, string(b1[:n1]))
// You can also Seek to a known location in the file and Read from there
o2, err := f.Seek(6, io.SeekStart)
check(err)
b2 := make([]byte, 2)
n2, err := f.Read(b2)
check(err)
fmt.Printf("%d bytes: @ %d: ", n2, o2)
fmt.Printf("%v\n", string(b2[:n2]))
// Other methods of seeking are relative to the current cursor position
_, err = f.Seek(4, io.SeekCurrent)
check(err)
// and relative to the end of the file
_, err = f.Seek(-10, io.SeekEnd)
check(err)
// The io package provides some functions that may be helpful for
// file reading. For example, reads like the ones above can be more
// robustly implemented with ReadAtLeast.
o3, err := f.Seek(6, io.SeekStart)
check(err)
b3 := make([]byte, 2)
n3, err := io.ReadAtLeast(f, b3, 2)
check(err)
fmt.Printf("%d bytes @ %d: %s\n", n3, o3, string(b3))
// There is no built-in rewind, but Seek(0, io.SeekStart) accomplishes this
_, err = f.Seek(0, io.SeekStart)
check(err)
// The bufio package implements a buffered reader that may be useful
// both for its efficiency with many small reads and because of the
// additional reading methods it provides.
r4 := bufio.NewReader(f)
b4, err := r4.Peek(5)
check(err)
fmt.Printf("5 bytes: %s\n", string(b4))
// Close the file when you're done (usually this would be scheduled
// immediately after Opening with defer)
f.Close()
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/vmalloc/s-go-example.git
git@gitee.com:vmalloc/s-go-example.git
vmalloc
s-go-example
s-go-example
master

搜索帮助