gf/os/gfile/gfile_z_example_contents_test.go

241 lines
5.5 KiB
Go
Raw Normal View History

2021-11-17 14:14:19 +08:00
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
2021-11-05 14:30:50 +08:00
package gfile_test
import (
"fmt"
"github.com/gogf/gf/v2/os/gfile"
)
func ExampleGetContents() {
// init
2021-11-11 13:01:32 +08:00
var (
fileName = "gflie_example.txt"
tempDir = gfile.TempDir("gfile_example_content")
tempFile = gfile.Join(tempDir, fileName)
)
2021-11-05 14:30:50 +08:00
// write contents
2021-11-11 13:01:32 +08:00
gfile.PutContents(tempFile, "goframe example content")
2021-11-05 14:30:50 +08:00
2021-11-17 19:50:03 +08:00
// It reads and returns the file content as string.
// It returns empty string if it fails reading, for example, with permission or IO error.
2021-11-11 13:01:32 +08:00
fmt.Println(gfile.GetContents(tempFile))
2021-11-05 14:30:50 +08:00
// Output:
2021-11-11 13:01:32 +08:00
// goframe example content
2021-11-05 14:30:50 +08:00
}
func ExampleGetBytes() {
// init
2021-11-11 13:01:32 +08:00
var (
fileName = "gflie_example.txt"
tempDir = gfile.TempDir("gfile_example_content")
tempFile = gfile.Join(tempDir, fileName)
)
2021-11-05 14:30:50 +08:00
// write contents
2021-11-11 13:01:32 +08:00
gfile.PutContents(tempFile, "goframe example content")
2021-11-05 14:30:50 +08:00
2021-11-17 19:50:03 +08:00
// It reads and returns the file content as []byte.
// It returns nil if it fails reading, for example, with permission or IO error.
2021-11-11 13:01:32 +08:00
fmt.Println(gfile.GetBytes(tempFile))
2021-11-05 14:30:50 +08:00
// Output:
2021-11-11 13:01:32 +08:00
// [103 111 102 114 97 109 101 32 101 120 97 109 112 108 101 32 99 111 110 116 101 110 116]
2021-11-05 14:30:50 +08:00
}
func ExamplePutContents() {
// init
2021-11-11 13:01:32 +08:00
var (
fileName = "gflie_example.txt"
tempDir = gfile.TempDir("gfile_example_content")
tempFile = gfile.Join(tempDir, fileName)
)
2021-11-05 14:30:50 +08:00
2021-11-17 19:50:03 +08:00
// It creates and puts content string into specifies file path.
// It automatically creates directory recursively if it does not exist.
2021-11-11 13:01:32 +08:00
gfile.PutContents(tempFile, "goframe example content")
2021-11-05 14:30:50 +08:00
// read contents
2021-11-11 13:01:32 +08:00
fmt.Println(gfile.GetContents(tempFile))
2021-11-05 14:30:50 +08:00
// Output:
2021-11-11 13:01:32 +08:00
// goframe example content
2021-11-05 14:30:50 +08:00
}
func ExamplePutBytes() {
// init
2021-11-11 13:01:32 +08:00
var (
fileName = "gflie_example.txt"
tempDir = gfile.TempDir("gfile_example_content")
tempFile = gfile.Join(tempDir, fileName)
)
2021-11-05 14:30:50 +08:00
// write contents
2021-11-11 13:01:32 +08:00
gfile.PutBytes(tempFile, []byte("goframe example content"))
2021-11-05 14:30:50 +08:00
// read contents
2021-11-11 13:01:32 +08:00
fmt.Println(gfile.GetContents(tempFile))
2021-11-05 14:30:50 +08:00
// Output:
2021-11-11 13:01:32 +08:00
// goframe example content
2021-11-05 14:30:50 +08:00
}
func ExamplePutContentsAppend() {
// init
2021-11-11 13:01:32 +08:00
var (
fileName = "gflie_example.txt"
tempDir = gfile.TempDir("gfile_example_content")
tempFile = gfile.Join(tempDir, fileName)
)
2021-11-05 14:30:50 +08:00
// write contents
2021-11-11 13:01:32 +08:00
gfile.PutContents(tempFile, "goframe example content")
2021-11-05 14:30:50 +08:00
// read contents
2021-11-11 13:01:32 +08:00
fmt.Println(gfile.GetContents(tempFile))
2021-11-05 14:30:50 +08:00
2021-11-19 09:53:32 +08:00
// It creates and append content string into specifies file path.
// It automatically creates directory recursively if it does not exist.
2021-11-11 13:01:32 +08:00
gfile.PutContentsAppend(tempFile, " append content")
2021-11-05 14:30:50 +08:00
// read contents
2021-11-11 13:01:32 +08:00
fmt.Println(gfile.GetContents(tempFile))
2021-11-05 14:30:50 +08:00
// Output:
2021-11-11 13:01:32 +08:00
// goframe example content
// goframe example content append content
2021-11-05 14:30:50 +08:00
}
func ExamplePutBytesAppend() {
// init
2021-11-11 13:01:32 +08:00
var (
fileName = "gflie_example.txt"
tempDir = gfile.TempDir("gfile_example_content")
tempFile = gfile.Join(tempDir, fileName)
)
2021-11-05 14:30:50 +08:00
// write contents
2021-11-11 13:01:32 +08:00
gfile.PutContents(tempFile, "goframe example content")
2021-11-05 14:30:50 +08:00
// read contents
2021-11-11 13:01:32 +08:00
fmt.Println(gfile.GetContents(tempFile))
2021-11-05 14:30:50 +08:00
// write contents
gfile.PutBytesAppend(tempFile, []byte(" append"))
// read contents
2021-11-11 13:01:32 +08:00
fmt.Println(gfile.GetContents(tempFile))
2021-11-05 14:30:50 +08:00
// Output:
2021-11-11 13:01:32 +08:00
// goframe example content
// goframe example content append
2021-11-05 14:30:50 +08:00
}
func ExampleGetNextCharOffsetByPath() {
// init
2021-11-11 13:01:32 +08:00
var (
fileName = "gflie_example.txt"
tempDir = gfile.TempDir("gfile_example_content")
tempFile = gfile.Join(tempDir, fileName)
)
2021-11-05 14:30:50 +08:00
// write contents
2021-11-11 13:01:32 +08:00
gfile.PutContents(tempFile, "goframe example content")
2021-11-05 14:30:50 +08:00
// read contents
2021-11-11 13:01:32 +08:00
index := gfile.GetNextCharOffsetByPath(tempFile, 'f', 0)
2021-11-05 14:30:50 +08:00
fmt.Println(index)
// Output:
2021-11-11 13:01:32 +08:00
// 2
2021-11-05 14:30:50 +08:00
}
func ExampleGetBytesTilCharByPath() {
// init
2021-11-11 13:01:32 +08:00
var (
fileName = "gflie_example.txt"
tempDir = gfile.TempDir("gfile_example_content")
tempFile = gfile.Join(tempDir, fileName)
)
2021-11-05 14:30:50 +08:00
// write contents
2021-11-11 13:01:32 +08:00
gfile.PutContents(tempFile, "goframe example content")
2021-11-05 14:30:50 +08:00
// read contents
2021-11-11 13:01:32 +08:00
fmt.Println(gfile.GetBytesTilCharByPath(tempFile, 'f', 0))
2021-11-05 14:30:50 +08:00
// Output:
2021-11-11 13:01:32 +08:00
// [103 111 102] 2
2021-11-05 14:30:50 +08:00
}
func ExampleGetBytesByTwoOffsetsByPath() {
// init
2021-11-11 13:01:32 +08:00
var (
fileName = "gflie_example.txt"
tempDir = gfile.TempDir("gfile_example_content")
tempFile = gfile.Join(tempDir, fileName)
)
2021-11-05 14:30:50 +08:00
// write contents
2021-11-11 13:01:32 +08:00
gfile.PutContents(tempFile, "goframe example content")
2021-11-05 14:30:50 +08:00
// read contents
2021-11-11 13:01:32 +08:00
fmt.Println(gfile.GetBytesByTwoOffsetsByPath(tempFile, 0, 7))
2021-11-05 14:30:50 +08:00
// Output:
2021-11-11 13:01:32 +08:00
// [103 111 102 114 97 109 101]
2021-11-05 14:30:50 +08:00
}
func ExampleReadLines() {
// init
2021-11-11 13:01:32 +08:00
var (
fileName = "gflie_example.txt"
tempDir = gfile.TempDir("gfile_example_content")
tempFile = gfile.Join(tempDir, fileName)
)
2021-11-05 14:30:50 +08:00
// write contents
2021-11-11 13:01:32 +08:00
gfile.PutContents(tempFile, "L1 goframe example content\nL2 goframe example content")
2021-11-05 14:30:50 +08:00
// read contents
gfile.ReadLines(tempFile, func(text string) error {
// Process each line
fmt.Println(text)
return nil
})
// Output:
2021-11-11 13:01:32 +08:00
// L1 goframe example content
// L2 goframe example content
2021-11-05 14:30:50 +08:00
}
func ExampleReadLinesBytes() {
// init
2021-11-11 13:01:32 +08:00
var (
fileName = "gflie_example.txt"
tempDir = gfile.TempDir("gfile_example_content")
tempFile = gfile.Join(tempDir, fileName)
)
2021-11-05 14:30:50 +08:00
// write contents
2021-11-11 13:01:32 +08:00
gfile.PutContents(tempFile, "L1 goframe example content\nL2 goframe example content")
2021-11-05 14:30:50 +08:00
// read contents
gfile.ReadLinesBytes(tempFile, func(bytes []byte) error {
// Process each line
2021-11-11 13:01:32 +08:00
fmt.Println(bytes)
2021-11-05 14:30:50 +08:00
return nil
})
// Output:
2021-11-11 13:01:32 +08:00
// [76 49 32 103 111 102 114 97 109 101 32 101 120 97 109 112 108 101 32 99 111 110 116 101 110 116]
// [76 50 32 103 111 102 114 97 109 101 32 101 120 97 109 112 108 101 32 99 111 110 116 101 110 116]
2021-11-05 14:30:50 +08:00
}