formatter

package
v2.3.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 7, 2026 License: MIT Imports: 13 Imported by: 7

Documentation

Overview

Package formatter implements some functions to format string, struct.

Package formatter implements some functions to format string, struct.

Index

Examples

Constants

This section is empty.

Variables

View Source
var A1Data = map[int]*Region{
	1:  {ID: 1, PID: 0, Name: "北京"},
	2:  {ID: 2, PID: 0, Name: "天津"},
	3:  {ID: 3, PID: 0, Name: "河北省"},
	4:  {ID: 4, PID: 0, Name: "山西省"},
	5:  {ID: 5, PID: 0, Name: "内蒙古自治区"},
	6:  {ID: 6, PID: 0, Name: "辽宁省"},
	7:  {ID: 7, PID: 0, Name: "吉林省"},
	8:  {ID: 8, PID: 0, Name: "黑龙江省"},
	9:  {ID: 9, PID: 0, Name: "上海"},
	10: {ID: 10, PID: 0, Name: "江苏省"},
	11: {ID: 11, PID: 0, Name: "浙江省"},
	12: {ID: 12, PID: 0, Name: "安徽省"},
	13: {ID: 13, PID: 0, Name: "福建省"},
	14: {ID: 14, PID: 0, Name: "江西省"},
	15: {ID: 15, PID: 0, Name: "山东省"},
	16: {ID: 16, PID: 0, Name: "河南省"},
	17: {ID: 17, PID: 0, Name: "湖北省"},
	18: {ID: 18, PID: 0, Name: "湖南省"},
	19: {ID: 19, PID: 0, Name: "广东省"},
	20: {ID: 20, PID: 0, Name: "广西壮族自治区"},
	21: {ID: 21, PID: 0, Name: "海南省"},
	22: {ID: 22, PID: 0, Name: "重庆"},
	23: {ID: 23, PID: 0, Name: "四川省"},
	24: {ID: 24, PID: 0, Name: "贵州省"},
	25: {ID: 25, PID: 0, Name: "云南省"},
	26: {ID: 26, PID: 0, Name: "西藏自治区"},
	27: {ID: 27, PID: 0, Name: "陕西省"},
	28: {ID: 28, PID: 0, Name: "甘肃省"},
	29: {ID: 29, PID: 0, Name: "青海省"},
	30: {ID: 30, PID: 0, Name: "宁夏回族自治区"},
	31: {ID: 31, PID: 0, Name: "新疆维吾尔自治区"},
	32: {ID: 32, PID: 0, Name: "台湾"},
	33: {ID: 33, PID: 0, Name: "香港特别行政区"},
	34: {ID: 34, PID: 0, Name: "澳门特别行政区"},
	35: {ID: 35, PID: 0, Name: "海外"},
}

A1Data 省级数据

View Source
var A2Data = map[int]*Region{}/* 367 elements not displayed */

A2Data 市级数据(完整数据)

View Source
var A3Data = map[int]*Region{}/* 3653 elements not displayed */

A3Data 区县级数据(完整数据)

Functions

func BinaryBytes added in v2.1.19

func BinaryBytes(size float64, precision ...int) string

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

func DecimalBytes(size float64, precision ...int) string

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

func ParseBinaryBytes(size string) (uint64, error)

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

func ParseDecimalBytes(size string) (uint64, error)

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

func Pretty(v any) (string, error)

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

func PrettyToWriter(v any, out io.Writer) error

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

type AddressInfo added in v2.3.9

type AddressInfo struct {
	Name     string `json:"name"`     // Name of the recipient / 姓名
	Mobile   string `json:"mobile"`   // Mobile phone number or landline / 手机号或座机
	IDN      string `json:"idn"`      // ID card number / 身份证号
	Postcode string `json:"postcode"` // Postal code / 邮编
	Province string `json:"province"` // Province / 省
	City     string `json:"city"`     // City / 市
	Region   string `json:"region"`   // District or county / 区/县
	Street   string `json:"street"`   // Street address / 街道详细地址
	Addr     string `json:"addr"`     // Original address string / 原始地址字符串
}

AddressInfo represents the parsed address information including user details and location. AddressInfo 表示解析后的地址信息,包括用户详细信息和位置信息

func ParseCNAddress added in v2.3.9

func ParseCNAddress(str string, withUser bool) *AddressInfo

ParseCNAddress parses a Chinese address string intelligently and extracts structured information. It can parse addresses with or without user information (name, phone, ID card, etc.). When withUser is true, it extracts user information from the address string. When withUser is false, it only parses the location information. The function handles various address formats including: - Standard format: "Province City District Street" - Compact format: "Name Phone Province City District Street" - With keywords: "Name: xxx Phone: xxx Address: xxx" - County-level cities: "Province City CountyCity District" (e.g., "河北省石家庄市新乐市") ParseCNAddress 智能解析中国地址字符串并提取结构化信息。 可以解析带或不带用户信息(姓名、电话、身份证等)的地址。 当 withUser 为 true 时,从地址字符串中提取用户信息。 当 withUser 为 false 时,仅解析位置信息。 该函数处理多种地址格式,包括: - 标准格式:"省 市 区 街道" - 紧凑格式:"姓名 电话 省 市 区 街道" - 带关键词:"姓名:xxx 电话:xxx 地址:xxx" - 县级市:"省 市 县级市 区"(如"河北省石家庄市新乐市")

Example
// 解析包含用户信息的完整地址
result := ParseCNAddress("张三 13800138000 北京市朝阳区建国路1号", true)
jsonData, _ := json.MarshalIndent(result, "", "  ")
println(string(jsonData))

func ParsePersonInfo added in v2.3.9

func ParsePersonInfo(str string) *AddressInfo

ParsePersonInfo extracts user information (name, phone, ID card, postal code) from an address string. It separates personal information from the address, supporting various formats: - Labeled format: "Name: xxx Phone: xxx Address: xxx" - Compact format: "Name Phone Address" (e.g., "张三13800138000北京市朝阳区") - With separators: using colons, commas, newlines as delimiters Returns an AddressInfo with extracted user information and cleaned address string in Addr field. ParsePersonInfo 从地址字符串中提取用户信息(姓名、电话、身份证、邮编)。 将个人信息与地址分离,支持多种格式: - 带标签格式:"姓名:xxx 电话:xxx 地址:xxx" - 紧凑格式:"姓名 电话 地址"(如"张三13800138000北京市朝阳区") - 带分隔符:使用冒号、逗号、换行符作为分隔符 返回包含提取的用户信息和清理后地址字符串(在 Addr 字段中)的 AddressInfo。

Example
// 分离用户信息
result := ParsePersonInfo("收货人:李四 电话:18612345678 地址:上海市浦东新区世纪大道100号")
jsonData, _ := json.MarshalIndent(result, "", "  ")
println(string(jsonData))

type Region added in v2.3.9

type Region struct {
	ID   int    `json:"id"`
	PID  int    `json:"pid"`  // 父级ID
	Name string `json:"name"` // 地区名称
}

Region 地区数据结构

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL