fmtx

package
v0.0.0-...-adcb032 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2023 License: Apache-2.0 Imports: 2 Imported by: 4

README

fmtx

GoDoc

Package fmtx contains formatting utilities. It means to be a complement to the standard fmt package.

Documentation

Overview

Package fmtx contains formatting utilities. It means to be a complement to the standard fmt package.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CondSprintf

func CondSprintf(format string, v ...interface{}) string

CondSprintf is like fmt.Sprintf(), but extra arguments (that have no verb in the format string) are ignored (not treated as an error).

Usually mismatching format string and arguments is an indication of a bug in your code (in how you call fmt.Sprintf()), so you should not overuse this.

For details, see https://stackoverflow.com/a/59696492/1705598

Example

ExampleFormatInt shows how to use the CondSprintf() function.

fmt.Println(CondSprintf("Foo%s", "bar", "baz"))
fmt.Println(CondSprintf("%d + %d = %d", 1, 2, 3, "extra", 4))
Output:

Foobar
1 + 2 = 3

func FormatInt

func FormatInt(n int64, groupSize int, grouping byte) string

FormatInt formats an integer with grouping decimals, in decimal radix. Grouping signs are inserted after every groupSize digits, starting from the right. A groupingSize less than 1 will default to 3. Only ASCII grouping decimal signs are supported which may be provided with grouping.

For details, see https://stackoverflow.com/a/31046325/1705598

Example

ExampleFormatInt shows how to use the FormatInt() function.

fmt.Println("groupSize:                  3               4")
fmt.Println("---------------------------------------------")
for _, v := range []int64{12, 123, 1234, 123456789} {
	for sign := int64(1); sign >= -1; sign -= 2 {
		x := v * sign
		fmt.Printf("n: %10s", fmt.Sprint(x))
		for groupingSize := 3; groupingSize <= 4; groupingSize++ {
			fmt.Printf(" =%14s", FormatInt(x, groupingSize, ','))
		}
		fmt.Println()
	}
}
Output:

groupSize:                  3               4
---------------------------------------------
n:         12 =            12 =            12
n:        -12 =           -12 =           -12
n:        123 =           123 =           123
n:       -123 =          -123 =          -123
n:       1234 =         1,234 =          1234
n:      -1234 =        -1,234 =         -1234
n:  123456789 =   123,456,789 =   1,2345,6789
n: -123456789 =  -123,456,789 =  -1,2345,6789

func FormatSize

func FormatSize(size int64, unit SizeUnit, fractionDigits int) string

FormatSize formats the given size value using the given size unit, rounding to the given number of fraction digits. Fraction digits are omitted when the (resulting) unit is SizeUnitByte.

If SizeUnitAuto is specified, the unit will be automatically selected based on the size value.

Behavior for negative size values is undefined.

Types

type SizeUnit

type SizeUnit string

SizeUnit is the type of the unit sizes

const (
	// SizeUnitAuto indicates that another unit is to be chosen automatically
	// based on the size value
	SizeUnitAuto SizeUnit = "(auto)"
	// SizeUnitByte is the byte unit size
	SizeUnitByte SizeUnit = "bytes"
	// SizeUnitKB is the kilobyte (2^10 bytes) unit size
	SizeUnitKB SizeUnit = "KB"
	// SizeUnitMB is the megabyte (2^20 bytes) unit size
	SizeUnitMB SizeUnit = "MB"
	// SizeUnitGB is the gigabyte (2^30 bytes) unit size
	SizeUnitGB SizeUnit = "GB"
	// SizeUnitTB is the terabyte (2^40 bytes) unit size
	SizeUnitTB SizeUnit = "TB"
	// SizeUnitPB is the petabyte (2^50 bytes) unit size
	SizeUnitPB SizeUnit = "PB"
	// SizeUnitEB is the exabyte (2^60 bytes) unit size
	SizeUnitEB SizeUnit = "EB"
)

Jump to

Keyboard shortcuts

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