Go string format
The verb at the end defines the type and the interpretation of its corresponding argument.
d- decimal integero- octal integerO- octal integer with0oprefixb- binary integerx- hexadecimal integer lowercaseX- hexadecimal integer uppercasef- decimal floating point, lowercaseF- decimal floating point, uppercasee- scientific notation (mantissa/exponent), lowercaseE- scientific notation (mantissa/exponent), uppercaseg- the shortest representation of%eor%fG- the shortest representation of%Eor%Fc- a character represented by the corresponding Unicode code pointq- a quoted characterU- Unicode escape sequencet- the word true or falses- a stringv- default format#v- Go-syntax representation of the valueT- a Go-syntax representation of the type of the valuep- pointer address%- a double%%prints a single%
Go string format indexing
package main
import (
"fmt"
)
func main() {
n1 := 2
n2 := 3
n3 := 4
res := fmt.Sprintf("There are %d oranges %d apples %d plums", n1, n2, n3)
fmt.Println(res) // There are 2 oranges 3 apples 4 plums
res2 := fmt.Sprintf("There are %[2]d oranges %d apples %[1]d plums", n1, n2, n3)
fmt.Println(res2) // There are 3 oranges 4 apples 2 plums
}
Go string format precision
package main
import (
"fmt"
)
func main() {
fmt.Printf("%0.f\n", 16.540) // 17
fmt.Printf("%0.2f\n", 16.540) // 16.54
fmt.Printf("%0.3f\n", 16.540) // 16.540
fmt.Printf("%0.5f\n", 16.540) // 16.54000
}
Go string format flags
package main
import (
"fmt"
)
func main() {
fmt.Printf("%+d\n", 1691) // +1691
fmt.Printf("%#x\n", 1691) // 0x69b
fmt.Printf("%#X\n", 1691) // 0X69B
fmt.Printf("%#b\n", 1691) // 0b11010011011
fmt.Printf("%10d\n", 1691) // 1691
fmt.Printf("%-10d\n", 1691) // 1691
fmt.Printf("%010d\n", 1691) // 0000001691
}
Go string format width
package main
import (
"fmt"
)
func main() {
w := "falcon"
n := 122
h := 455.67
fmt.Printf("%s\n", w) // falcon
fmt.Printf("%10s\n", w) // falcon
fmt.Printf("%d\n", n). // 122
fmt.Printf("%7d\n", n) // 122
fmt.Printf("%07d\n", n) // 0000122
fmt.Printf("%10f\n", h) // 455.670000
fmt.Printf("%11f\n", h) // 455.670000
fmt.Printf("%12f\n", h) // 455.670000
}
