Go fmt
The fmt package implements formatted I/O similar to C's printf and scanf, primarily categorized into outputting content and acquiring input.
Outputting Content
Print series
The Print series of functions outputs content to the system's standard output.
//Output content as-is to os.Stdout
func Print(a ...any) (n int, err error)
//Output formatted content to os.Stdout
func Printf(format string, a ...any) (n int, err error)
//Output content as-is with a newline character appended to os.Stdout
func Println(a ...any) (n int, err error)
Fprint series
The Fprint series of functions outputs content to a variable w that implements the io.Writer interface, commonly used for writing content to files.
//Output content as-is to w
func Fprint(w io.Writer, a ...any) (n int, err error)
//Output formatted content to w
func Fprintf(w io.Writer, format string, a ...any) (n int, err error)
//Output content as-is with a newline character appended to w
func Fprintln(w io.Writer, a ...any) (n int, err error)
Sprint series
The Sprint series of functions generate and return a string from the input data.
//Return content as-is
func Sprint(a ...any) string
//Return formatted content
func Sprintf(format string, a ...any) string
//Return content as-is with a newline character appended
func Sprintln(a ...any) string
Acquiring Input
The fmt package provides three functions: fmt.Scan, fmt.Scanf, and fmt.Scanln, for acquiring user input from the standard input during program execution.
//fmt.Scan scans text read from standard input, separating the values of the arguments by whitespace
func Scan(a ...any) (n int, err error)
//fmt.Scanf scans text read from standard input, separating the values of the arguments according to a specified format
func Scanf(format string, a ...any) (n int, err error)
//fmt.Scanln is similar to Scan, but stops scanning at a newline character
func Scanln(a ...any) (n int, err error)
var (
name string
age int
)
fmt.Scanf("name:%s\t\t\tage:%d", &name, &age)
fmt.Printf("Scanning results name:%s age:%d \n", name, age)
#name:flasle age:18
#Scanning results name:flasle age:18
Formatting Placeholders
Types | Placeholders | Meaning | Example |
General | %v | Default format representation of the value | fmt.Printf("%v\n", 100) //100 |
%+v | Similar to %v๏ผ but without adding field names for structs | o := struct{ name string }{"Flasle"} fmt.Printf("%+v\n", o) //{name:Flasle} | |
%#v | Go syntax representation of the value | //struct { name string }{name:"Flasle"} fmt.Printf("%#v\n", o) | |
%T | Print the type of the value | //struct { name string } fmt.Printf("%T\n", o) | |
%% | Literal percent sign | fmt.Printf("100%%\n") //100% | |
Boolean | %t | true or false | fmt.Printf("%t\n", true) //true |
Integer | %b | Binary representation of the value | n := 65 fmt.Printf("%b\n", n) //1000001 |
%c | ASCII character representation of the value | fmt.Printf("%c\n", n) //A | |
%d | Decimal representation of the value | fmt.Printf("%d\n", n) //65 | |
%o | Octal representation of the value | fmt.Printf("%o\n", n) //101 | |
%x | Hexadecimal representation, using a-f | fmt.Printf("%x\n", n) //41 | |
%X | Hexadecimal representation, using A-F | fmt.Printf("%X\n", n) //41 | |
%U | Unicode format representation, with prefix U+ | fmt.Printf("%U", '้ช') //U+95EA | |
Float | %e | Scientific notation, e.g., -1234.456e+78 | f := 13.14 fmt.Printf("%e\n", f) //1.314000e+01 |
%E | Scientific notation, e.g., -1234.456E+78 | fmt.Printf("%E\n", f) //1.314000E+01 | |
%f | Standard notation, floating-point representation with decimal point Default width, default precision | fmt.Printf("%f\n", f) //13.140000 | |
%F | equivalent to %f | fmt.Printf("%F\n", f) //13.140000 | |
%g | Uses %e or %f based on the situation for a concise, accurate output | fmt.Printf("%g\n", f) //13.14 | |
%G | Uses %E or %F based on the situation for a concise, accurate output | fmt.Printf("%G\n", f) //13.14 | |
String | %s | Directly outputs the string or []byte | |
Pointer | %p | Hexadecimal representation with prefix 0x | s := "Flasle" fmt.Printf("%p", &s) //0xc0004523e0 |
Width and Precision | %6f | Width 6, default precision | f := 13.14 fmt.Printf("%6f\n", f) //13.140000 |
%.2f | Default width, precision 2 (two digits after decimal point) | fmt.Printf("%.2f\n", f) //13.14 | |
%6.2f | Width 6, precision 2 Pads with spaces on the left if width is greater than the number of characters | fmt.Printf("%6.2f\n", f) // 13.14 | |
%6.f | Width 6, precision 0 (no decimal point) | fmt.Printf("%6.f\n", f) // 13 |
Fscan Series, Sscan Series, ...
Take a break
๐๐๐ ใJade Dynasty EP52ใXiao Bai fight scene of saving Gui Li