Formatter
formatter contains some functions for data formatting.
Source:
- https://github.com/duke-git/lancet/blob/main/formatter/formatter.go
- https://github.com/duke-git/lancet/blob/main/formatter/byte.go
Usage:
import (
"github.com/duke-git/lancet/v2/formatter"
)
Index
Documentation
Comma
Add comma to a number value by every 3 numbers from right to left. ahead by a prefix symbol char. if value is a invalid number string like "aa", return empty string.
Signature:
func Comma[T constraints.Float | constraints.Integer | string](value T, prefixSymbol string) string
Example:Run
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/formatter"
)
func main() {
result1 := formatter.Comma("123", "")
result2 := formatter.Comma("12345", "$")
result3 := formatter.Comma(1234567, "¥")
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Output:
// 123
// $12,345
// ¥1,234,567
}
Pretty
Pretty data to JSON string.
Signature:
func Pretty(v any) (string, error)
Example:Run
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/formatter"
)
func main() {
result1, _ := formatter.Pretty([]string{"a", "b", "c"})
result2, _ := formatter.Pretty(map[string]int{"a": 1})
fmt.Println(result1)
fmt.Println(result2)
// Output:
// [
// "a",
// "b",
// "c"
// ]
// {
// "a": 1
// }
}
PrettyToWriter
Pretty encode data to writer.
Signature:
func PrettyToWriter(v any, out io.Writer) error
Example:Run
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/formatter"
)
func main() {
type User struct {
Name string `json:"name"`
Aage uint `json:"age"`
}
user := User{Name: "King", Aage: 10000}
buf := &bytes.Buffer{}
err := formatter.PrettyToWriter(user, buf)
fmt.Println(buf)
fmt.Println(err)
// Output:
// {
// "name": "King",
// "age": 10000
// }
//
// <nil>
}
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 is 4 for default.
Signature:
func DecimalBytes(size float64, precision ...int) string
Example:Run
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/formatter"
)
func main() {
result1 := formatter.DecimalBytes(1000)
result2 := formatter.DecimalBytes(1024)
result3 := formatter.DecimalBytes(1234567)
result4 := formatter.DecimalBytes(1234567, 3)
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
// Output:
// 1KB
// 1.024KB
// 1.2346MB
// 1.235MB
}
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 is 4 for default.
Signature:
func BinaryBytes(size float64, precision ...int) string
Example:Run
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/formatter"
)
func main() {
result1 := formatter.BinaryBytes(1024)
result2 := formatter.BinaryBytes(1024 * 1024)
result3 := formatter.BinaryBytes(1234567)
result4 := formatter.BinaryBytes(1234567, 2)
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
// Output:
// 1KiB
// 1MiB
// 1.1774MiB
// 1.18MiB
}
ParseDecimalBytes
Returns the human readable bytes size string into the amount it represents(base 1000).
Signature:
func ParseDecimalBytes(size string) (uint64, error)
Example:Run
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/formatter"
)
func main() {
result1, _ := formatter.ParseDecimalBytes("12")
result2, _ := formatter.ParseDecimalBytes("12k")
result3, _ := formatter.ParseDecimalBytes("12 Kb")
result4, _ := formatter.ParseDecimalBytes("12.2 kb")
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
// Output:
// 12
// 12000
// 12000
// 12200
}
ParseBinaryBytes
Returns the human readable bytes size string into the amount it represents(base 1024).
Signature:
func ParseBinaryBytes(size string) (uint64, error)
Example:Run
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/formatter"
)
func main() {
result1, _ := formatter.ParseBinaryBytes("12")
result2, _ := formatter.ParseBinaryBytes("12ki")
result3, _ := formatter.ParseBinaryBytes("12 KiB")
result4, _ := formatter.ParseBinaryBytes("12.2 kib")
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
// Output:
// 12
// 12288
// 12288
// 12492
}