String Formatting

Wikipedia: printf format string
fmt.sprintf formats a string using Go-style verbs and returns it. fmt.printf writes the formatted output directly.

fmt := import("fmt")
fmt.println(fmt.sprintf("Hello, %s!", "Tengo"))
fmt.println(fmt.sprintf("%d + %d = %d", 3, 4, 7))
fmt.println(fmt.sprintf("Pi ~ %.4f", 3.14159))
fmt.println(fmt.sprintf("|%10s|%-10s|", "right", "left"))
fmt.println(fmt.sprintf("%v", [1, 2, 3]))
fmt.println(fmt.sprintf("%v", {a: 1}))
fmt.println(fmt.sprintf("%08b", 42))
fmt.println(fmt.sprintf("%x", 255))
fmt.printf("formatted: %d\n", 99)

try it

Hello, Tengo!
3 + 4 = 7
Pi ~ 3.1416
|     right|left      |
[1, 2, 3]
{a: 1}
00101010
ff
formatted: 99
loading…