Documentation ¶
Overview ¶
Package formatter implements some functions to format string, struct.
Index ¶
- Constants
- func BinaryBytes(size float64, precision ...int) string
- func Comma[T constraints.Float | constraints.Integer | string](value T, symbol string) string
- func DecimalBytes(size float64, precision ...int) string
- func ParseBinaryBytes(size string) (uint64, error)
- func ParseDecimalBytes(size string) (uint64, error)
- func Pretty(v any) (string, error)
- func PrettyToWriter(v any, out io.Writer) error
Examples ¶
Constants ¶
const ( // Decimal UnitB = 1 UnitKB = 1000 UnitMB = 1000 * UnitKB UnitGB = 1000 * UnitMB UnitTB = 1000 * UnitGB UnitPB = 1000 * UnitTB UnitEB = 1000 * UnitPB // Binary UnitBiB = 1 UnitKiB = 1024 UnitMiB = 1024 * UnitKiB UnitGiB = 1024 * UnitMiB UnitTiB = 1024 * UnitGiB UnitPiB = 1024 * UnitTiB UnitEiB = 1024 * UnitPiB )
Variables ¶
This section is empty.
Functions ¶
func BinaryBytes ¶
BinaryBytes returns a human-readable byte size under binary standard (base 1024) The precision parameter specifies the number of digits after the decimal point, which defaults to 4. Play: todo
Example ¶
result1 := BinaryBytes(1024) result2 := BinaryBytes(1024 * 1024) result3 := BinaryBytes(1234567) result4 := BinaryBytes(1234567, 2) fmt.Println(result1) fmt.Println(result2) fmt.Println(result3) fmt.Println(result4)
Output: 1KiB 1MiB 1.1774MiB 1.18MiB
func Comma ¶
func Comma[T constraints.Float | constraints.Integer | string](value T, symbol string) string
Comma add comma to a number value by every 3 numbers from right. ahead by symbol char. if value is invalid number string eg "aa", return empty string Comma("12345", "$") => "$12,345", Comma(12345, "$") => "$12,345" Play: https://go.dev/play/p/eRD5k2vzUVX
Example ¶
result1 := Comma("123", "") result2 := Comma("12345", "$") result3 := Comma(1234567, "¥") fmt.Println(result1) fmt.Println(result2) fmt.Println(result3)
Output: 123 $12,345 ¥1,234,567
func DecimalBytes ¶
DecimalBytes returns a human readable byte size under decimal standard (base 1000) The precision parameter specifies the number of digits after the decimal point, which defaults to 4. Play: todo
Example ¶
result1 := DecimalBytes(1000) result2 := DecimalBytes(1024) result3 := DecimalBytes(1234567) result4 := DecimalBytes(1234567, 3) fmt.Println(result1) fmt.Println(result2) fmt.Println(result3) fmt.Println(result4)
Output: 1KB 1.024KB 1.2346MB 1.235MB
func ParseBinaryBytes ¶
ParseBinaryBytes return the human readable bytes size string into the amount it represents(base 1024). ParseBinaryBytes("42 mib") -> 44040192, nil Play: todo
Example ¶
result1, _ := ParseBinaryBytes("12") result2, _ := ParseBinaryBytes("12ki") result3, _ := ParseBinaryBytes("12 KiB") result4, _ := ParseBinaryBytes("12.2 kib") fmt.Println(result1) fmt.Println(result2) fmt.Println(result3) fmt.Println(result4)
Output: 12 12288 12288 12492
func ParseDecimalBytes ¶
ParseDecimalBytes return the human readable bytes size string into the amount it represents(base 1000). ParseDecimalBytes("42 MB") -> 42000000, nil Play: todo
Example ¶
result1, _ := ParseDecimalBytes("12") result2, _ := ParseDecimalBytes("12k") result3, _ := ParseDecimalBytes("12 Kb") result4, _ := ParseDecimalBytes("12.2 kb") fmt.Println(result1) fmt.Println(result2) fmt.Println(result3) fmt.Println(result4)
Output: 12 12000 12000 12200
func Pretty ¶
Pretty data to JSON string. Play: todo
Example ¶
result1, _ := Pretty([]string{"a", "b", "c"}) result2, _ := Pretty(map[string]int{"a": 1}) fmt.Println(result1) fmt.Println(result2)
Output: [ "a", "b", "c" ] { "a": 1 }
func PrettyToWriter ¶
PrettyToWriter pretty encode data to writer. Play: todo
Example ¶
type User struct { Name string `json:"name"` Aage uint `json:"age"` } user := User{Name: "King", Aage: 10000} buf := &bytes.Buffer{} err := PrettyToWriter(user, buf) fmt.Println(buf) fmt.Println(err)
Output: { "name": "King", "age": 10000 } <nil>
Types ¶
This section is empty.