Golang - fmt包


格式化輸出

示例

package main

import (
	"fmt"
	"os"
)

func main() {
	name := "ky"

	// 將格式化後的字符串輸出給文件, f 爲文件句柄
	fmt.Fprintf(f, "%v\n", name)

	// 將格式化的字符串輸出到終端, 將文件句柄固定爲os.stdout
	fmt.Printf("%v\n", name)

	// 將格式化後的字符串返回
	a := fmt.Sprintf("my name is %v", name)
}


源碼

// 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
}

// 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...)
}

// 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
}


格式化參數

package main

import "fmt"

func main() {
	// %v 通用
	num := 18
	fmt.Printf("%v\n", num) // 18

	// %T 打印類型
	fmt.Printf("%T\n" num) // int

	// 指針
	fmt.Printf("%p\n", &s1)  // 帶0x的指針
	fmt.Printf("%#p\n", &s1) // 不帶0x的指針

	// %% 百分號
	fmt.Printf("%d%%\n", num) // 95%

	// 打印進制
	b := 15
	// %b 二進制, %d 十進制, %o 八進制, %x 十六進制
	fmt.Printf("%[1]b %[1]d %[1]o %[1]x\n", b)
	
	// 布爾值
	m := true
	fmt.Printf("%t\n", m)
	
	// 支持索引使用
	m := "a"
	n := "b"

	// 直接使用索引
	fmt.Printf("%[1]v,%[1]v,%[1]v,%[2]v\n", m, n) // a,a,a,b

	// 重新使用索引1後, 後邊的順序會重置
	fmt.Printf("%v,%v,%[1]v,%v\n", m, n) // 
}


int和float 相關用法

package main

import "fmt"

func main() {
	// 帶符號的整型
	a := +15
	b := -15
	fmt.Printf("%+d, %+d\n", a, b) // +15, -15, 類型都爲int

	// 指定長度的整型
	fmt.Printf("|%5d|\n", a)  // |    1|, 共5位, 默認右對齊
	fmt.Printf("|%-5d|\n", a) // |1    |, 共5位, 左對齊
	fmt.Printf("|%05d|\n", a) // |00001|, 共5位, 在前邊補0

	// 特殊用法
	m := 123
	n := 5
	fmt.Printf("%[1]*[2]d\n", a, pi) // 總寬度爲5, 結果爲  123(左數2個空格)

	// %f
	x := 3.1415926 
	y := 3.14

	fmt.Printf("%f, %f\n", x, y) // 3.141593 3.140000 默認保留6位小數, 會四捨五入
	fmt.Printf("%5.f\n", x)       // 視爲%5.0f, 總寬度爲5, 小數點後保留0位, 結果爲    3(左數4個空格)
	fmt.Printf("%5.2f\n", x)      // 總寬度爲5, 小數點後保留2位, 結果爲 3.14(左數一個空格)
	fmt.Printf("%.3f\n", x)       // 保留3位小數
	fmt.Printf("%.2g\n", x)       // 共出現兩個數字

	// 特殊用法
	fmt.Printf("%[1]*.[2]*[3]f\n", 5, 1, x) // 等同於 %5.1f, 由於使用了索引, 使用[]*區分, [num]* 表示寬度或者精度
}


string 相關用法

package main

import "fmt"

func main(){
	// 字符串
	s1 := "這是個字符串\"string1\""
	fmt.Printf("s1: %s\n", s1) // s1: 這是個字符串"string1"
	fmt.Printf("s1: %q\n", s1) // s1: "這是個字符串\"string1\""

	// 字符串跟長度
	fmt.Printf("s1: %20s\n", s1)    // 最小寬度爲20
	fmt.Printf("s1: %-20s\n", s1)   // 最小寬度爲20, 左對齊
	fmt.Printf("s1: %20.28s\n", s1) // 最小寬度.最大寬度, 默認右對齊
	fmt.Printf("s1: %-5.7s\n", s1)  // 左對齊
	fmt.Printf("s1: %020s\n", s1)   // 小於20前邊補0
}


捕獲輸入

package main

import "fmt"

func main() {
	var (
		name string
		age  int
	)

	// 將捕獲到的輸入賦值給指針, 沒有提示, 可以使用空格或者回車表示輸入下一個
	fmt.Scan(&name, &age)
	fmt.Println(name, age)

	// 必須按照這個格式去寫才能捕獲成功
	fmt.Scanf("name:%s age:%d\n", &name, &age)
	fmt.Println(name, age)

	// 只能使用空格, 回車則表示輸入完成
	fmt.Scanln(&name, &age)
	fmt.Println(name, age)
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章