fmtfmt

1、fmt 包

fmtprintfscanf$GOROOT\src\pkg\fmt
doc.go
errors.go
format.go
print.go
scan.go

1.1 常用函数

该包主要实现了字符串格式支持的读写功能,常用的函数有:

FprintFprintfFprintlnSprintSprintfSprintlnPrintPrintfPrintlnFscanFscanfFscanlnSscanSscanfSscanln

上述函数长的都很相似,很容易混淆、用错,在这里介绍一些方法方便区分它们。

Fio.Writerio.ReaderSfln\n

1.2 格式化占位符

fmt

普通占位符

定义一个结构体Person,并赋值:

type Person struct {
	Name string
}
var p = new(Person)
p.Name = "xcbeyond"
%vfmt.Printf("%v", p.Name)xcbeyond%+vfmt.Printf("%+v\n", p)&{Name:xcbeyond}%#vfmt.Printf("%#v\n", p)&main.Person{Name:"xcbeyond"}%Tfmt.Printf("%T\n", p.Name)string%%fmt.Printf("%%\n")%

布尔占位符

占位符%ttruefalsefmt.Printf("%t",1 == 1)true

整数占位符

%bfmt.Printf("%b", 5)%cUnicodefmt.Printf("%c", 0x4E2D)%dfmt.Printf("%d", 0x12)%ofmt.Printf("%d", 10)%qfmt.Printf("%q", 0x4E2D)%xfmt.Printf("%x", 13)%Xfmt.Printf("%x", 13)%UUnicodeU+1234U+%04Xfmt.Printf("%U", 0x4E2D)U+4E2D

复数(实部、虚部)占位符

%e%.3efmt.Printf("%.3e\n", math.Pi)3.142e+00%Efmt.Printf("%E\n", math.Pi)3.141593E+00%ffmt.Printf("%.3f\n", math.Pi)3.142%g%e%ffmt.Printf("%.3g\n", math.Pi)3.14%G%E%ffmt.Printf("%.3G\n", 10.20+5i)(10.2+5i)

字符串占位符

□:表示一个空格

%sfmt.Printf("%s\n", "xcbeyond")xcbeyond%10sfmt.Printf("%10s\n", "xcbeyond")□□xcbeyond%-10sfmt.Printf("%-10s\n", "xcbeyond")xcbeyond□□%.5sfmt.Printf("%.5s\n", "xcbeyond")xcbey%5.10sfmt.Printf("%5.10s\n", "xcbeyond")xcbeyond%-5.10sfmt.Printf("%-5.10s\n", "xcbeyond")xcbeyond%5.3sfmt.Printf("%5.3s\n", "xcbeyond")□□xcb%010sfmt.Printf("%010s\n", "xcbeyond")00xcbeyond%qfmt.Printf("%q\n", "xcbeyond")"xcbeyond"%xfmt.Printf("%x\n", "xcbeyond")78636265796f6e64%Xfmt.Printf("%X\n", "xcbeyond")78636265796F6E64

指针占位符

%p0xfmt.Printf("%p\n", &p)0xc00011a020%#p0xfmt.Printf("%#p\n", &p)c00011a020

2、输出

$GOROOT\src\fmt\print.go

2.1 fmt.Print

fmt.Print
// Print formats using the default formats for its operands and writes to standard output.
// Spaces are added between operands when neither is a string.
// It returns the number of bytes written and any write error encountered.
func Print(a ...interface{}) (n int, err error) {
	return Fprint(os.Stdout, a...)
}

示例如下:

name := "xcbeyond"
fmt.Print(name)	// xcbeyond

2.2 fmt.Printf

fmt.Printf
// Printf formats according to a format specifier and writes to standard output.
// It returns the number of bytes written and any write error encountered.
func Printf(format string, a ...interface{}) (n int, err error) {
	return Fprintf(os.Stdout, format, a...)
}

示例如下:

name := "xcbeyond"
fmt.Printf("%.5s", name)	// xcbey

2.3 fmt.Println

fmt.Println
// Println formats using the default formats for its operands and writes to standard output.
// Spaces are always added between operands and a newline is appended.
// It returns the number of bytes written and any write error encountered.
func Println(a ...interface{}) (n int, err error) {
	return Fprintln(os.Stdout, a...)
}

示例如下:

name := "xcbeyond"
fmt.Println(name)	// xcbeyond 换行

2.4 fmt.Sprint

fmt.Sprint
// Sprint formats using the default formats for its operands and returns the resulting string.
// Spaces are added between operands when neither is a string.
func Sprint(a ...interface{}) string {
	p := newPrinter()
	p.doPrint(a)
	s := string(p.buf)
	p.free()
	return s
}

示例如下:

name := "xcbeyond"
retStr := fmt.Sprint(name)
fmt.Print(retStr)	// xcbeyond

2.5 fmt.Sprintf

fmt.Sprintf
// Sprintf formats according to a format specifier and returns the resulting string.
func Sprintf(format string, a ...interface{}) string {
	p := newPrinter()
	p.doPrintf(format, a)
	s := string(p.buf)
	p.free()
	return s
}

示例如下:

name := "xcbeyond"
retStr2 := fmt.Sprintf("%.5s", name)
fmt.Print(retStr2) // xcbey

2.6 fmt.Sprintln

fmt.Sprintln
// Sprintln formats using the default formats for its operands and returns the resulting string.
// Spaces are always added between operands and a newline is appended.
func Sprintln(a ...interface{}) string {
	p := newPrinter()
	p.doPrintln(a)
	s := string(p.buf)
	p.free()
	return s
}

示例如下:

name := "xcbeyond"
retStr3 := fmt.Sprintln(name)
fmt.Print(retStr3) // xcbeyond  换行

2.7 fmt.Fprint

// Fprint formats using the default formats for its operands and writes to w.
// Spaces are added between operands when neither is a string.
// It returns the number of bytes written and any write error encountered.
func Fprint(w io.Writer, a ...interface{}) (n int, err error) {
	p := newPrinter()
	p.doPrint(a)
	n, err = w.Write(p.buf)
	p.free()
	return
}

2.8 fmt.Fprintf

// Fprintf formats according to a format specifier and writes to w.
// It returns the number of bytes written and any write error encountered.
func Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error) {
	p := newPrinter()
	p.doPrintf(format, a)
	n, err = w.Write(p.buf)
	p.free()
	return
}

2.9 fmt.Fprintln

// Fprintln formats using the default formats for its operands and writes to w.
// Spaces are always added between operands and a newline is appended.
// It returns the number of bytes written and any write error encountered.
func Fprintln(w io.Writer, a ...interface{}) (n int, err error) {
	p := newPrinter()
	p.doPrintln(a)
	n, err = w.Write(p.buf)
	p.free()
	return
}

3、输入

3.1 fmt.Fscan

3.2 fmt.Fscanf

3.3 fmt.Fscanln

3.4 fmt.Scan

3.5 fmt.Scanf

3.6 fmt.Scanln

3.7 fmt.Sscan

3.8 fmt.Sscanf

3.9 fmt.Sscanln

参考资料: