You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
2.1 KiB

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"
}