iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 9
0
自我挑戰組

golang初探系列 第 9

day9-fmt Package使用介紹

  • 分享至 

  • twitterImage
  •  

先前範例當中使用 fmt package ,這個章節獨立介紹 fmt 如何使用

fmt

在官方介紹當中說明了 fmt package 實現了格式化 I/O 方法
類似於 C 語言 prtinf 和 scanf 方法
使用上更簡單

通用

%v the value in a default format when printing structs, the plus flag (%+v) adds field names
%#v a Go-syntax representation of the value
%T a Go-syntax representation of the type of the value
%% a literal percent sign; consumes no value

藉由官方文檔的解釋,我們來用程式碼了解

package main

import (
	"fmt"
)

func main() {
	var n = 100
	fmt.Printf("%v\n", n)        //值的默認格式
	fmt.Printf("%#v\n", n)       //該值在 Go 表示
	fmt.Printf("%T\n", n)        //使用%T 可以查看類型
	fmt.Printf("%%+v %+v\n", n) //%%輸出 % 字串

}

Boolean

example
將 boolean 轉換成字串方式顯示出來

%t the word true or false

package main

import (
	"fmt"
)

func main() {
	var a bool
	fmt.Printf("%t", a) //轉換字串的 true 或 false

}

Integer

example

%b base 2
%c the character represented by the corresponding Unicode code point
%d base 10
%o base 8
%O base 8 with 0o prefix
%q a single-quoted character literal safely escaped with Go syntax.
%x base 16, with lower-case letters for a-f
%X base 16, with upper-case letters for A-F
%U Unicode format: U+1234; same as "U+%04X"

package main

import (
	"fmt"
)

func main() {
	var a = 100

	fmt.Printf("%b\n", a) //轉換二進制
	fmt.Printf("%c\n", a) //轉換至 Unicode 字符 100 對應 ascii 為 d 
	fmt.Printf("%d\n", a) //轉換十進制
	fmt.Printf("%o\n", a) //轉換八進制
	fmt.Printf("%O\n", a) //轉換八進制用0o表示
	fmt.Printf("%q\n", a) //轉換Unicode 字符並以單引號方式表示
	fmt.Printf("%x\n", a) //轉換16進制 用小寫 a-f 表示
	fmt.Printf("%X\n", a) //轉換16進制 用小寫 A-F 表示
	fmt.Printf("%U\n", a) //轉成 Unicode 格式
}

Floating-point and complex constituents:

%b decimalless scientific notation with exponent a power of two,
in the manner of strconv.FormatFloat with the 'b' format,
e.g. -123456p-78
%e scientific notation, e.g. -1.234456e+78
%E scientific notation, e.g. -1.234456E+78
%f decimal point but no exponent, e.g. 123.456
%F synonym for %f
%g %e for large exponents, %f otherwise. Precision is discussed below.
%G %E for large exponents, %F otherwise
%x hexadecimal notation (with decimal power of two exponent), e.g. -0x1.23abcp+20
%X upper-case hexadecimal notation, e.g. -0X1.23ABCP+20

package main

import (
	"fmt"
)

func main() {
	f := 12.34
	fmt.Printf("%b\n", f) //無小數部分,二進制指數的科學記號
	fmt.Printf("%e\n", f) //使用e科學記號
	fmt.Printf("%E\n", f) //使用E科學記號
	fmt.Printf("%f\n", f) //有小數,但無指數部分
	fmt.Printf("%F\n", f) //如%f
	fmt.Printf("%g\n", f) //更據實際情況採用 %e 或 %f 格式
	fmt.Printf("%G\n", f) //更據實際情況採用 %E 或 %F 格式
}

String and slice of bytes (treated equivalently with these verbs):

example

package main

import (
	"fmt"
)

func main() {
	var a = "t"

	fmt.Printf("%s\n", a)//輸出字串
	fmt.Printf("%q\n", a) //使用雙引號進行表達
	fmt.Printf("%x\n", a) //每個字符用兩個byte 16進制表達 (a-z)
	fmt.Printf("%X\n", a) //每個字符用兩個byte 16進制表達 (A-Z)

}

Width is specified by an optional decimal number immediately preceding the verb. If absent, the width is whatever is necessary to represent the value. Precision is specified after the (optional) width by a period followed by a decimal number. If no period is present, a default precision is used. A period with no following number specifies a precision of zero. Examples:

%f default width, default precision
%9f width 9, default precision
%.2f default width, precision 2
%9.2f width 9, precision 2
%9.f width 9, precision 0

package main

import (
	"fmt"
)

func main() {
	f := 12.34
	fmt.Printf("%f\n", f) //默認精度
	fmt.Printf("%9f\n", f) //寬度9,默認精度
	fmt.Printf("%.2f\n", f) //默認寬度,精度2
	fmt.Printf("%9.2f\n", f) //寬度9,精度2
	fmt.Printf("%9.f\n", f) //寬度9,精度0

}

上一篇
day8-布林值及資料類型轉換
下一篇
day10-指標(Pointer)使用
系列文
golang初探30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言