Go Package fmt: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 120: Line 120:
fmt.Sprintf("%*s", 10, "test")
fmt.Sprintf("%*s", 10, "test")
</syntaxhighlight>
</syntaxhighlight>
==Dynamic Format Strings==
==Numerical Value part of the Conversion Character as Argument of the Function==
If the number of padding characters or other configurable value in the format string comes as a variable, the format string can be built dynamically as follows and then used in a <code>fmt.Printf()</code> function:
If the number of padding characters or other configurable value in a conversion character is variable, the following <code>*</code> syntax can be used:
<syntaxhighlight lang='go'>
<syntaxhighlight lang='go'>
zeroPaddingCount := 4
zeroPaddingCount := 4
formatString := fmt.Sprintf("%%0%dd", zeroPaddingCount)
i := 1
i := 1
fmt.Printf(formatString, i) // will print 0001
fmt.Printf("%0*d", zeroPaddingCount, i) // will print 0001
</syntaxhighlight>
</syntaxhighlight>



Revision as of 22:49, 25 July 2024

External

Internal

Formatting

A format string, or a template, is text interspersed with conversion characters.

Conversion Characters

The conversion characters are also known as placeholders.

%v

Render the value as string in the default format.

"%v" and fmt.Errorf() is typically used in the error handling idiom that involves error annotation.

%+v

When printing structs, the plus flag %+v adds field names.

%#v

A Go-syntax representation of the value.

%#v can be used to show that an error instance returned by errors.New("something") is a pointer:

&errors.errorString{s:"something"}

%T

A Go-syntax representation of the type of the value.

%%

A literal percent sign. Consumes no value.

Strings

color := "blue"
fmt.Printf("The color is %s\n", color)

Integers

size := 1
fmt.Printf("The size is %d\n", size)

Characters

c := 'x'
fmt.Printf("The character is %c\n", c)

Booleans

b := true
fmt.Printf("The boolean value is %t\n", b)

Floating Point Numbers

%b

Decimalless scientific notation with exponent a power of two, in the manner of strconv.FormatFloat with the 'b' format, e.g. -123456p-78

%e

Scientific notation, e.g. -1.234456e+78

%E

Scientific notation, e.g. -1.234456E+78

%f

Decimal point but no exponent, e.g. 123.456

f := 1.0
fmt.Printf("The floating point value is %f\n", f)

Limit the number of decimals:

fmt.Printf("The floating point value is %.2f\n", f)

If the number of decimals comes as a variable, see Dynamic Format Strings.

%F

Synonym for %f.

%g

%e for large exponents, %f otherwise.

%G

%E for large exponents, %F otherwise

%x

Hexadecimal notation (with decimal power of two exponent), e.g. -0x1.23abcp+20

%X

Upper-case hexadecimal notation, e.g. -0X1.23ABCP+20

%w

Used with fmt.Errorf() to wrap errors.

Pointers

Pointers in Go | Displaying Pointers

Structs

Go Structs | Printing Structs

Padding

Leading Zero Padding for Integers

i := 7
s := fmt.Sprintf("%06d", i) // will produce "000007" (five zeroes)

If the number of padding characters comes as a variable, see Dynamic Format Strings.

Leading Space Padding for Integer

i := 7
s := fmt.Sprintf("%6d", i) // will produce "     7" (five spaces)

If the number of padding characters comes as a variable, see Dynamic Format Strings.

String Padding

Left:

fmt.Sprintf("%10s", "test")

Right:

fmt.Sprintf("%-10s", "test")

Padding quantity specified as parameter of the function:

fmt.Sprintf("%*s", 10, "test")

Numerical Value part of the Conversion Character as Argument of the Function

If the number of padding characters or other configurable value in a conversion character is variable, the following * syntax can be used:

zeroPaddingCount := 4
i := 1
fmt.Printf("%0*d", zeroPaddingCount, i) // will print 0001

Functions

Sprintf()

Format a string and returns it as a result:

message := fmt.Sprintf("Hi, %v. Welcome!", name)

For more details on the format string, see:

Printf() Format String

Printf(), Println()

Printing to stdout and stderr

Scanf(), Scanln()

Handling stdin in Go

Errorf()

Go Language Error Handling

Interfaces

fmt.Stringer

type Stringer interface {
  String() string
}

Stringer is implemented by any type that has a String() method, which defines the "native" format for that value. The String() method is used to print values passed as an operand to any format that accepts a string or to an unformatted printer such as Print. For a usage example, see:

The Equivalent of Java toString() in Go