新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)GoFrame教程:GoFramegfile-內(nèi)容管理
內(nèi)容管理
GetContents
- 說明:讀取指定路徑文件內(nèi)容,以字符串形式返回。
- 格式:
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
}
GetContentsWithCache
- 說明:帶緩存獲取文件內(nèi)容,可設(shè)置緩存超時,文件發(fā)生變化自動清除緩存。
- 格式:
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
}
GetBytesWithCache
- 說明:帶緩存獲取文件內(nèi)容,可設(shè)置緩存超時,文件發(fā)生變化自動清除緩存,返回?[]byte?。
- 格式:
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]
}
GetBytes
- 說明:讀取指定路徑文件內(nèi)容,以字節(jié)形式返回。
- 格式:
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]
}
GetBytesTilChar
- 說明:以某個字符定位截取指定長度的文件內(nèi)容以字節(jié)形式返回
- 格式:
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
}
GetBytesByTwoOffsets
- 說明:以指定的區(qū)間讀取文件內(nèi)容
- 格式:
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]
}
PutContents
- 說明:往指定路徑文件添加字符串內(nèi)容。如果文件不存在將會遞歸的形式自動創(chuàng)建。
- 格式:
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
}
PutBytes
- 說明:以字節(jié)形式寫入指定文件,如果文件不存在將會遞歸的形式自動創(chuàng)建
- 格式:
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
}
PutContentsAppend
- 說明:追加字符串內(nèi)容到指定文件,如果文件不存在將會遞歸的形式自動創(chuàng)建。
- 格式:
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
}
PutBytesAppend
- 說明:追加字節(jié)內(nèi)容到指定文件。如果文件不存在將會遞歸的形式自動創(chuàng)建。
- 格式:
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
}
GetNextCharOffset
- 說明:從某個偏移量開始,獲取文件中指定字符所在下標(biāo)
- 格式:
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
}
GetNextCharOffsetByPath
- 說明:從某個偏移量開始,獲取文件中指定字符所在下標(biāo)
- 格式:
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
}
GetBytesTilCharByPath
- 說明:以某個字符定位截取指定長度的文件內(nèi)容以字節(jié)形式返回
- 格式:
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
}
GetBytesByTwoOffsetsByPath
- 說明:用兩個偏移量截取指定文件的內(nèi)容以字節(jié)形式返回
- 格式:
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]
}
ReadLines
- 說明:以字符串形式逐行讀取文件內(nèi)容
- 格式:
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
}
ReadLinesBytes
- 說明:以字節(jié)形式逐行讀取文件內(nèi)容
- 格式:
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]
}
Truncate
- 說明:裁剪文件為指定大小
- 注意:如果給定文件路徑是軟鏈,將會修改源文件
- 格式:
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
}

創(chuàng)新互聯(lián)是專業(yè)的玉泉網(wǎng)站建設(shè)公司,玉泉接單;提供成都網(wǎng)站制作、做網(wǎng)站,網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行玉泉網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!
當(dāng)前標(biāo)題:創(chuàng)新互聯(lián)GoFrame教程:GoFramegfile-內(nèi)容管理
URL分享:http://m.fisionsoft.com.cn/article/djiphpe.html


咨詢
建站咨詢
