init
All checks were successful
continuous-integration/drone/tag Build is passing

This commit is contained in:
2024-10-04 14:55:21 +03:00
parent 01de72c729
commit 779818e067
9 changed files with 775 additions and 0 deletions

76
example_test.go Normal file
View File

@@ -0,0 +1,76 @@
package bytesize_test
import (
"encoding/json"
"fmt"
"gitoa.ru/go-4devs/bytesize"
)
func ExampleParse() {
size, err := bytesize.Parse("100kB")
fmt.Printf("100kB:%[1]T(%[1]d),%[2]v\n", size, err)
size, err = bytesize.Parse("100PB")
fmt.Printf("100PB:%[1]T(%[1]d),%[2]v\n", size, err)
size, err = bytesize.Parse("100KiB")
fmt.Printf("100PB:%[1]T(%[1]d),%[2]v\n", size, err)
size, err = bytesize.Parse("100MiB")
fmt.Printf("100PB:%[1]T(%[1]d),%[2]v\n", size, err)
size, err = bytesize.Parse("100B")
fmt.Printf("100PB:%[1]T(%[1]d),%[2]v\n", size, err)
// Output:
// 100kB:bytesize.Size(100000),<nil>
// 100PB:bytesize.Size(100000000000000000),<nil>
// 100PB:bytesize.Size(102400),<nil>
// 100PB:bytesize.Size(104857600),<nil>
// 100PB:bytesize.Size(100),<nil>
}
func ExampleSize_String() {
fmt.Printf("%[1]T(%[1]d) = %[1]s\n", bytesize.Size(100000))
fmt.Printf("%[1]T(%[1]d) = %[1]s\n", bytesize.Size(100000000000000000))
fmt.Printf("%[1]T(%[1]d) = %[1]s\n", bytesize.Size(102400))
fmt.Printf("%[1]T(%[1]d) = %[1]s\n", bytesize.Size(104857600))
fmt.Printf("%[1]T(%[1]d) = %[1]s\n", bytesize.Size(100))
// Output:
// bytesize.Size(100000) = 100kB
// bytesize.Size(100000000000000000) = 100PB
// bytesize.Size(102400) = 102.4kB
// bytesize.Size(104857600) = 104.8576MB
// bytesize.Size(100) = 100B
}
func ExampleSize_jsonMarshal() {
size := bytesize.Size(100000)
data, _ := json.Marshal(size)
fmt.Printf("%[1]T(%[1]d) = %[2]s\n", size, data)
size = bytesize.Size(100000000000000000)
data, _ = json.Marshal(size)
fmt.Printf("%[1]T(%[1]d) = %[2]s\n", size, data)
size = bytesize.Size(102400)
data, _ = json.Marshal(size)
fmt.Printf("%[1]T(%[1]d) = %[2]s\n", size, data)
size = bytesize.Size(104857600)
data, _ = json.Marshal(size)
fmt.Printf("%[1]T(%[1]d) = %[2]s\n", size, data)
size = bytesize.Size(100)
data, _ = json.Marshal(size)
fmt.Printf("%[1]T(%[1]d) = %[2]s\n", size, data)
// Output:
// bytesize.Size(100000) = "100kB"
// bytesize.Size(100000000000000000) = "100PB"
// bytesize.Size(102400) = "102.4kB"
// bytesize.Size(104857600) = "104.8576MB"
// bytesize.Size(100) = "100B"
}