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

TypesPlaceholdersMeaningExample
General%vDefault format representation of the valuefmt.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}

%#vGo syntax representation of the value

//struct { name string }{name:"Flasle"}

fmt.Printf("%#v\n", o) 

%TPrint the type of the value

 //struct { name string }

fmt.Printf("%T\n", o)

%%Literal percent signfmt.Printf("100%%\n")  //100%
Boolean%ttrue or falsefmt.Printf("%t\n", true)  //true
Integer%bBinary representation of the value

n := 65

fmt.Printf("%b\n", n) //1000001

%cASCII character representation of the valuefmt.Printf("%c\n", n) //A
%dDecimal representation of the valuefmt.Printf("%d\n", n) //65
%oOctal representation of the valuefmt.Printf("%o\n", n) //101
%xHexadecimal representation, using a-ffmt.Printf("%x\n", n) //41
%XHexadecimal representation, using A-Ffmt.Printf("%X\n", n) //41
%U

Unicode format representation, 

with prefix U+

fmt.Printf("%U", '้—ช') //U+95EA
Float%eScientific notation, e.g., -1234.456e+78

f := 13.14

fmt.Printf("%e\n", f) //1.314000e+01

%EScientific notation, e.g., -1234.456E+78fmt.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
%Fequivalent to %ffmt.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%sDirectly outputs the string or []byte 
Pointer%pHexadecimal representation with prefix 0x

s := "Flasle"

fmt.Printf("%p", &s) //0xc0004523e0

Width and Precision%6fWidth 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.fWidth 6, precision 0 (no decimal point)fmt.Printf("%6.f\n", f)  //    13

Fscan Series, Sscan Series, ...

Take a break

๐Ÿ‘‰๐Ÿ‘‰๐Ÿ‘‰ ใ€BTTH Year EP109ใ€‘Xiao Yixian fight scene and Xiao Yan arrived in time to rescue her