Go Fmt Printf
[ Go Language Basic Syntax](#)
fmt.Printf is a powerful output formatting function in Go, mainly used to format strings and output the result to standard output (usually the console).
fmt.Printf formats subsequent variables according to the specified format string, with the following syntax:
fmt.Printf(format, arglist...)
* **format:** string form, format specifiers start with %, %s for string format, %d for decimal integer format.
* **arglist:** multiple parameters separated by commas, the number must match the number in the format, otherwise a runtime error will occur.
## Example
package main
import(
"fmt"
"strings"
)
func main(){
name :="John Doe"
age :=27
// Print string in lowercase
fmt.Printf("hello, %sn", strings.ToLower(name))
// Print string in uppercase
fmt.Printf("hello, %sn", strings.ToUpper(name))
// Print age as decimal number
fmt.Printf("%s is %d years oldn", name, age)
// Print age as floating point number with two decimal places
fmt.Printf("%s is %.2f years oldn", name, age)
}
Output:
hello, John Doe hello, JOHN DOE John Doe is 27 years old John Doe is %!f(int=27) years old
Go String Formatting Verbs:
| Formatting Verb | Description | Example |
| --- | --- | --- |
| **General** | | |
| `%v` | Output variable in default format | `fmt.Printf("%v", 42)` |
| `%+v` | Output struct with field names | `fmt.Printf("%+v", struct{A int}{A: 42})` |
| `%#v` | Output in Go syntax format | `fmt.Printf("%#v", mapint{"a": 1})` |
| `%T` | Output type of value | `fmt.Printf("%T", 42)` |
| `%%` | Output percent sign | `fmt.Printf("%%")` |
| **Boolean** | | |
| `%t` | Output `true` or `false` | `fmt.Printf("%t", true)` |
| **Integer** | | |
| `%b` | Binary representation | `fmt.Printf("%b", 5)` |
| `%c` | Unicode corresponding character | `fmt.Printf("%c", 65)` |
| `%d` | Decimal representation | `fmt.Printf("%d", 42)` |
| `%o` | Octal representation | `fmt.Printf("%o", 10)` |
| `%x` | Hexadecimal representation (lowercase) | `fmt.Printf("%x", 15)` |
| `%X` | Hexadecimal representation (uppercase) | `fmt.Printf("%X", 15)` |
| `%U` | Unicode format output | `fmt.Printf("%U", 65)` |
| **Floating Point** | | |
| `%f` | Decimal floating point | `fmt.Printf("%f", 3.14)` |
| `%e` | Scientific notation (lowercase e) | `fmt.Printf("%e", 3.14)` |
| `%E` | Scientific notation (uppercase E) | `fmt.Printf("%E", 3.14)` |
| `%g` | Compact representation using `%f` or `%e` | `fmt.Printf("%g", 3.14)` |
| **String and Byte** | | |
| `%s` | Plain string | `fmt.Printf("%s", "Go")` |
| `%q` | String or character with quotes | `fmt.Printf("%q", "Go")` |
| `%x` | Each byte represented as two hex characters | `fmt.Printf("%x", "abc")` |
| `%X` | Hexadecimal (uppercase) representation | `fmt.Printf("%X", "abc")` |
| **Pointer** | | |
| `%p` | Pointer address | `fmt.Printf("%p", &x)` |
A format string consists of regular text and format placeholders.
Format placeholders start with %, followed by one or more characters, indicating the formatting type.
The structure of a format placeholder is:
%[.precision]verb
* **flags:** Flags to control formatted output (optional).
* `-`: Left align.
* `+`: Always show sign of number.
* `0`: Pad with zeros.
* `#`: Add prefix for binary, octal, hexadecimal, etc.
* `space`: Add space before positive numbers, minus sign before negative numbers.
* **width:** Output width (optional).
* **.precision:** Number of decimal places for floating point (optional).
* **verb:** Specifies how to format the data.
**1. Format Integer**
fmt.Printf("Decimal: %d, Binary: %b, Octal: %o, Hex: %xn", 42, 42, 42, 42)// Output: Decimal: 42, Binary: 101010, Octal: 52, Hex: 2a
**2. Format Floating Point**
fmt.Printf("Normal: %f, Scientific: %e, Auto: %gn", 123.456, 123.456, 123.456)// Output: Normal: 123.456000, Scientific: 1.234560e+02, Auto: 123.456
**3. Format String**
fmt.Printf("Plain string: %s, Quoted: %q, Hex: %xn", "Go", "Go", "Go")// Output: Plain string: Go, Quoted: "Go", Hex: 476f
**4. Use Width and Alignment**
fmt.Printf("|%10s|%-10s|n", "right", "left")// Output: | right|left |
**5. Format Struct**
type User struct { Name string Age int} user := User{Name: "Alice", Age: 30} fmt.Printf("Default: %vnWith field names: %+vnGo syntax: %#vn", user, user, user)// Output: // Default: {Alice 30}// With field names: {Name:Alice Age:30}// Go syntax: main.User{Name:"Alice", Age:30}
**6. Format Boolean**
fmt.Printf("Boolean: %t, %tn", true, false)// Output: Boolean: true, false
## Example
package main
import(
"fmt"
"os"
)
type point struct{
x, y int
}
func main(){
p := point{1,2}
fmt.Printf("%vn", p)
fmt.Printf("%+vn", p)
fmt.Printf("%#vn", p)
fmt.Printf("%Tn", p)
fmt.Printf("%tn",true)
fmt.Printf("%dn",123)
fmt.Printf("%bn",14)
fmt.Printf("%cn",33)
fmt.Printf("%xn",456)
fmt.Printf("%fn",78.9)
fmt.Printf("%en
YouTip