github.com/wangyougui/gf/v2@v2.6.5/os/gfile/gfile_z_example_size_test.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/wangyougui/gf.
     6  
     7  package gfile_test
     8  
     9  import (
    10  	"fmt"
    11  
    12  	"github.com/wangyougui/gf/v2/os/gfile"
    13  )
    14  
    15  func ExampleSize() {
    16  	// init
    17  	var (
    18  		fileName = "gfile_example.txt"
    19  		tempDir  = gfile.Temp("gfile_example_size")
    20  		tempFile = gfile.Join(tempDir, fileName)
    21  	)
    22  
    23  	// write contents
    24  	gfile.PutContents(tempFile, "0123456789")
    25  	fmt.Println(gfile.Size(tempFile))
    26  
    27  	// Output:
    28  	// 10
    29  }
    30  
    31  func ExampleSizeFormat() {
    32  	// init
    33  	var (
    34  		fileName = "gfile_example.txt"
    35  		tempDir  = gfile.Temp("gfile_example_size")
    36  		tempFile = gfile.Join(tempDir, fileName)
    37  	)
    38  
    39  	// write contents
    40  	gfile.PutContents(tempFile, "0123456789")
    41  	fmt.Println(gfile.SizeFormat(tempFile))
    42  
    43  	// Output:
    44  	// 10.00B
    45  }
    46  
    47  func ExampleReadableSize() {
    48  	// init
    49  	var (
    50  		fileName = "gfile_example.txt"
    51  		tempDir  = gfile.Temp("gfile_example_size")
    52  		tempFile = gfile.Join(tempDir, fileName)
    53  	)
    54  
    55  	// write contents
    56  	gfile.PutContents(tempFile, "01234567899876543210")
    57  	fmt.Println(gfile.ReadableSize(tempFile))
    58  
    59  	// Output:
    60  	// 20.00B
    61  }
    62  
    63  func ExampleStrToSize() {
    64  	size := gfile.StrToSize("100MB")
    65  	fmt.Println(size)
    66  
    67  	// Output:
    68  	// 104857600
    69  }
    70  
    71  func ExampleFormatSize() {
    72  	sizeStr := gfile.FormatSize(104857600)
    73  	fmt.Println(sizeStr)
    74  	sizeStr0 := gfile.FormatSize(1024)
    75  	fmt.Println(sizeStr0)
    76  	sizeStr1 := gfile.FormatSize(999999999999999999)
    77  	fmt.Println(sizeStr1)
    78  
    79  	// Output:
    80  	// 100.00M
    81  	// 1.00K
    82  	// 888.18P
    83  }