func GetContents(path string) string
func ExampleGetContents() {
// init
var (
fileName = "gflie_example.txt"
tempDir = gfile.TempDir("gfile_example_content")
tempFile = gfile.Join(tempDir, fileName)
)
// write contents
gfile.PutContents(tempFile, "GOframe example content")
// It reads and returns the file content as string.
// It returns empty string if it fails reading, for example, with permission or IO error.
fmt.Println(gfile.GetContents(tempFile))
// Output:
// GoFrame example content
}
func GetContentsWithCache(path string, duration ...time.Duration) string
func ExampleGetContentsWithCache() {
// init
var (
fileName = "gflie_example.txt"
tempDir = gfile.TempDir("gfile_example_cache")
tempFile = gfile.Join(tempDir, fileName)
)
// write contents
gfile.PutContents(tempFile, "goframe example content")
// It reads the file content with cache duration of one minute,
// which means it reads from cache after then without any IO operations within on minute.
fmt.Println(gfile.GetContentsWithCache(tempFile, time.Minute))
// write new contents will clear its cache
gfile.PutContents(tempFile, "new goframe example content")
// There's some delay for cache clearing after file content change.
time.Sleep(time.Second * 1)
// read contents
fmt.Println(gfile.GetContentsWithCache(tempFile))
// May Output:
// goframe example content
// new goframe example content
}
func GetBytesWithCache(path string, duration ...time.Duration) []byte
func ExampleGetBytesWithCache() {
// init
var (
fileName = "gflie_example.txt"
tempDir = gfile.TempDir("gfile_example_cache")
tempFile = gfile.Join(tempDir, fileName)
)
// write contents
gfile.PutContents(tempFile, "goframe example content")
// It reads the file content with cache duration of one minute,
// which means it reads from cache after then without any IO operations within on minute.
fmt.Println(gfile.GetBytesWithCache(tempFile, time.Minute))
// write new contents will clear its cache
gfile.PutContents(tempFile, "new goframe example content")
// There's some delay for cache clearing after file content change.
time.Sleep(time.Second * 1)
// read contents
fmt.Println(gfile.GetBytesWithCache(tempFile))
// Output:
// [103 111 102 114 97 109 101 32 101 120 97 109 112 108 101 32 99 111 110 116 101 110 116]
// [110 101 119 32 103 111 102 114 97 109 101 32 101 120 97 109 112 108 101 32 99 111 110 116 101 110 116]
}
func GetBytes(path string) []byte
func ExampleGetBytes() {
// init
var (
fileName = "gflie_example.txt"
tempDir = gfile.TempDir("gfile_example_content")
tempFile = gfile.Join(tempDir, fileName)
)
// write contents
gfile.PutContents(tempFile, "goframe example content")
// It reads and returns the file content as []byte.
// It returns nil if it fails reading, for example, with permission or IO error.
fmt.Println(gfile.GetBytes(tempFile))
// Output:
// [103 111 102 114 97 109 101 32 101 120 97 109 112 108 101 32 99 111 110 116 101 110 116]
}
func GetBytesTilChar(reader io.ReaderAt, char byte, start int64) ([]byte, int64)
func ExampleGetBytesTilChar() {
// init
var (
fileName = "gflie_example.txt"
tempDir = gfile.TempDir("gfile_example_content")
tempFile = gfile.Join(tempDir, fileName)
)
// write contents
gfile.PutContents(tempFile, "goframe example content")
f, _ := gfile.OpenWithFlagPerm(tempFile, os.O_RDONLY, gfile.DefaultPermOpen)
// GetBytesTilChar returns the contents of the file as []byte
// until the next specified byte `char` position.
char, i := gfile.GetBytesTilChar(f, 'f', 0)
fmt.Println(char)
fmt.Println(i)
// Output:
// [103 111 102]
// 2
}
func GetBytesByTwoOffsets(reader io.ReaderAt, start int64, end int64) []byte
func ExampleGetBytesByTwoOffsets() {
// init
var (
fileName = "gflie_example.txt"
tempDir = gfile.TempDir("gfile_example_content")
tempFile = gfile.Join(tempDir, fileName)
)
// write contents
gfile.PutContents(tempFile, "goframe example content")
f, _ := gfile.OpenWithFlagPerm(tempFile, os.O_RDONLY, gfile.DefaultPermOpen)
// GetBytesTilChar returns the contents of the file as []byte
// until the next specified byte `char` position.
char := gfile.GetBytesByTwoOffsets(f, 0, 3)
fmt.Println(char)
// Output:
// [103 111 102]
}
func putContents(path string, data []byte, flag int, perm os.FileMode) error
func ExamplePutContents() {
// init
var (
fileName = "gflie_example.txt"
tempDir = gfile.TempDir("gfile_example_content")
tempFile = gfile.Join(tempDir, fileName)
)
// It creates and puts content string into specifies file path.
// It automatically creates directory recursively if it does not exist.
gfile.PutContents(tempFile, "goframe example content")
// read contents
fmt.Println(gfile.GetContents(tempFile))
// Output:
// goframe example content
}
func PutBytes(path string, content []byte) error
func ExamplePutBytes() {
// init
var (
fileName = "gflie_example.txt"
tempDir = gfile.TempDir("gfile_example_content")
tempFile = gfile.Join(tempDir, fileName)
)
// write contents
gfile.PutBytes(tempFile, []byte("goframe example content"))
// read contents
fmt.Println(gfile.GetContents(tempFile))
// Output:
// goframe example content
}
func PutContentsAppend(path string, content string) error
func ExamplePutContentsAppend() {
// init
var (
fileName = "gflie_example.txt"
tempDir = gfile.TempDir("gfile_example_content")
tempFile = gfile.Join(tempDir, fileName)
)
// write contents
gfile.PutContents(tempFile, "goframe example content")
// read contents
fmt.Println(gfile.GetContents(tempFile))
// It creates and append content string into specifies file path.
// It automatically creates directory recursively if it does not exist.
gfile.PutContentsAppend(tempFile, " append content")
// read contents
fmt.Println(gfile.GetContents(tempFile))
// Output:
// goframe example content
// goframe example content append content
}
func PutBytesAppend(path string, content []byte) error
func ExamplePutBytesAppend() {
// init
var (
fileName = "gflie_example.txt"
tempDir = gfile.TempDir("gfile_example_content")
tempFile = gfile.Join(tempDir, fileName)
)
// write contents
gfile.PutContents(tempFile, "goframe example content")
// read contents
fmt.Println(gfile.GetContents(tempFile))
// write contents
gfile.PutBytesAppend(tempFile, []byte(" append"))
// read contents
fmt.Println(gfile.GetContents(tempFile))
// Output:
// goframe example content
// goframe example content append
}
func GetNextCharOffset(reader io.ReaderAt, char byte, start int64) int64
func ExampleGetNextCharOffset() {
// init
var (
fileName = "gflie_example.txt"
tempDir = gfile.TempDir("gfile_example_content")
tempFile = gfile.Join(tempDir, fileName)
)
// write contents
gfile.PutContents(tempFile, "goframe example content")
f, err := gfile.OpenWithFlagPerm(tempFile, os.O_RDONLY, DefaultPermOpen)
defer f.Close()
// read contents
index := gfile.GetNextCharOffset(f, 'f', 0)
fmt.Println(index)
// Output:
// 2
}
func GetNextCharOffsetByPath(path string, char byte, start int64) int64
func ExampleGetNextCharOffsetByPath() {
// init
var (
fileName = "gflie_example.txt"
tempDir = gfile.TempDir("gfile_example_content")
tempFile = gfile.Join(tempDir, fileName)
)
// write contents
gfile.PutContents(tempFile, "goframe example content")
// read contents
index := gfile.GetNextCharOffsetByPath(tempFile, 'f', 0)
fmt.Println(index)
// Output:
// 2
}
func GetBytesTilCharByPath(path string, char byte, start int64) ([]byte, int64)
func ExampleGetBytesTilCharByPath() {
// init
var (
fileName = "gflie_example.txt"
tempDir = gfile.TempDir("gfile_example_content")
tempFile = gfile.Join(tempDir, fileName)
)
// write contents
gfile.PutContents(tempFile, "goframe example content")
// read contents
fmt.Println(gfile.GetBytesTilCharByPath(tempFile, 'f', 0))
// Output:
// [103 111 102] 2
}
func GetBytesByTwoOffsetsByPath(path string, start int64, end int64) []byte
func ExampleGetBytesByTwoOffsetsByPath() {
// init
var (
fileName = "gflie_example.txt"
tempDir = gfile.TempDir("gfile_example_content")
tempFile = gfile.Join(tempDir, fileName)
)
// write contents
gfile.PutContents(tempFile, "goframe example content")
// read contents
fmt.Println(gfile.GetBytesByTwoOffsetsByPath(tempFile, 0, 7))
// Output:
// [103 111 102 114 97 109 101]
}
func ReadLines(file string, callback func(text string) error) error
func ExampleReadLines() {
// init
var (
fileName = "gflie_example.txt"
tempDir = gfile.TempDir("gfile_example_content")
tempFile = gfile.Join(tempDir, fileName)
)
// write contents
gfile.PutContents(tempFile, "L1 goframe example content\nL2 goframe example content")
// read contents
gfile.ReadLines(tempFile, func(text string) error {
// Process each line
fmt.Println(text)
return nil
})
// Output:
// L1 goframe example content
// L2 goframe example content
}
func ReadLinesBytes(file string, callback func(bytes []byte) error) error
func ExampleReadLinesBytes() {
// init
var (
fileName = "gflie_example.txt"
tempDir = gfile.TempDir("gfile_example_content")
tempFile = gfile.Join(tempDir, fileName)
)
// write contents
gfile.PutContents(tempFile, "L1 goframe example content\nL2 goframe example content")
// read contents
gfile.ReadLinesBytes(tempFile, func(bytes []byte) error {
// Process each line
fmt.Println(bytes)
return nil
})
// Output:
// [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]
}
func Truncate(path string, size int) error
func ExampleTruncate(){
// init
var (
path = gfile.Join(gfile.TempDir("gfile_example_basic_dir"), "file1")
)
// Check whether the `path` size
stat, _ := gfile.Stat(path)
fmt.Println(stat.Size())
// Truncate file
gfile.Truncate(path, 0)
// Check whether the `path` size
stat, _ = gfile.Stat(path)
fmt.Println(stat.Size())
// Output:
// 13
// 0
}
创新互联是专业的玉泉网站建设公司,玉泉接单;提供成都网站制作、做网站,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行玉泉网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
当前标题:创新互联GoFrame教程:GoFramegfile-内容管理
路径分享:http://www.shufengxianlan.com/qtweb/news29/548729.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联