Files
gf/os/gfile/gfile_z_example_size_test.go

84 lines
1.6 KiB
Go
Raw Permalink 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 ExampleSize() {
2021-11-13 23:42:03 +08:00
// init
var (
fileName = "gflie_example.txt"
tempDir = gfile.Temp("gfile_example_size")
2021-11-13 23:42:03 +08:00
tempFile = gfile.Join(tempDir, fileName)
)
// write contents
gfile.PutContents(tempFile, "0123456789")
fmt.Println(gfile.Size(tempFile))
2021-11-05 14:30:50 +08:00
// Output:
2021-11-13 23:42:03 +08:00
// 10
2021-11-05 14:30:50 +08:00
}
func ExampleSizeFormat() {
2021-11-13 23:42:03 +08:00
// init
var (
fileName = "gflie_example.txt"
tempDir = gfile.Temp("gfile_example_size")
2021-11-13 23:42:03 +08:00
tempFile = gfile.Join(tempDir, fileName)
)
// write contents
gfile.PutContents(tempFile, "0123456789")
fmt.Println(gfile.SizeFormat(tempFile))
2021-11-05 14:30:50 +08:00
// Output:
2021-11-13 23:42:03 +08:00
// 10.00B
2021-11-05 14:30:50 +08:00
}
func ExampleReadableSize() {
2021-11-13 23:42:03 +08:00
// init
var (
fileName = "gflie_example.txt"
tempDir = gfile.Temp("gfile_example_size")
2021-11-13 23:42:03 +08:00
tempFile = gfile.Join(tempDir, fileName)
)
// write contents
gfile.PutContents(tempFile, "01234567899876543210")
fmt.Println(gfile.ReadableSize(tempFile))
2021-11-05 14:30:50 +08:00
// Output:
2021-11-13 23:42:03 +08:00
// 20.00B
2021-11-05 14:30:50 +08:00
}
func ExampleStrToSize() {
size := gfile.StrToSize("100MB")
fmt.Println(size)
// Output:
// 104857600
}
func ExampleFormatSize() {
sizeStr := gfile.FormatSize(104857600)
fmt.Println(sizeStr)
2021-11-13 23:42:03 +08:00
sizeStr0 := gfile.FormatSize(1024)
fmt.Println(sizeStr0)
2021-11-05 14:30:50 +08:00
sizeStr1 := gfile.FormatSize(999999999999999999)
fmt.Println(sizeStr1)
// Output:
// 100.00M
2021-11-13 23:42:03 +08:00
// 1.00K
2021-11-05 14:30:50 +08:00
// 888.18P
}