Documentation
¶
Overview ¶
Package formatter implements some functions to format string, struct.
Index ¶
- func BinaryBytes(size float64, precision ...int) string
- func Comma[T constraints.Float | constraints.Integer | string](value T, prefixSymbol 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 ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BinaryBytes ¶ added in v2.1.19
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: https://go.dev/play/p/G9oHHMCAZxP
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, prefixSymbol string) string
Comma add comma to a number value by every 3 numbers from right. ahead by prefix 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 ¶ added in v2.1.19
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: https://go.dev/play/p/FPXs1suwRcs
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 ¶ added in v2.1.19
ParseBinaryBytes return the human readable bytes size string into the amount it represents(base 1024). ParseBinaryBytes("42 mib") -> 44040192, nil Play: https://go.dev/play/p/69v1tTT62x8
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 ¶ added in v2.1.19
ParseDecimalBytes return the human readable bytes size string into the amount it represents(base 1000). ParseDecimalBytes("42 MB") -> 42000000, nil Play: https://go.dev/play/p/Am98ybWjvjj
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 ¶ added in v2.1.19
Pretty data to JSON string. Play: https://go.dev/play/p/YsciGj3FH2x
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 ¶ added in v2.1.19
PrettyToWriter pretty encode data to writer. Play: https://go.dev/play/p/LPLZ3lDi5ma
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.