Hello World程序實例
Go變量實例
Go常量實例
Go for循環語句實例
Go if/else語句實例
Go switch語句實例
Go切片實例
Go範圍實例
Go函數實例
Go函數多個返回值實例
Go可變參數的函數實例
Go閉包(匿名函數)實例
Go函數遞歸實例
Go指針實例
Go指針實例
Go接口實例
Go錯誤實例
Go程序實例
Go通道實例
Go通道緩衝實例
Go通道同步實例
Go通道路線實例
Go Select實例
Go超時(timeouts)實例
Go非阻塞通道操作實例
Go關閉通道實例
Go通道範圍實例
Go計時器實例
Go斷續器實例
Go工作池實例
Go速率限制實例
Go原子計數器實例
Go互斥體實例
Go有狀態的goroutines實例
Go排序實例
Go按自定義函數排序實例
Go panic錯誤處理實例
Go延遲(defer)實例
Go集合函數實例
Go字符串函數實例
Go字符串格式化實例
Go正則表達式實例
Go JSON實例
Go時間日期實例
Go時代(Epoch)實例
Go時間格式化/解析實例
Go隨機數實例
Go數字解析實例
Go URL解析實例
Go SHA1哈希實例
Go Base64編碼實例
Go讀取文件實例
Go寫文件實例
Go行過濾器實例
Go命令行參數實例
Go命令行標誌實例
Go環境變量實例
Go執行過程實例
Go信號實例
Go退出程序實例
Go字符串格式化實例
Go語言爲printf
傳統中的字符串格式化提供了極好的支持。 以下是常見字符串格式化任務的一些示例。
Go提供了幾種打印「動詞」,設計用於格式化一般的值。 例如,打印 point
結構的一個實例。
如果值是一個結構體,%+v
變體將包括結構體的字段名。
%#v
變體打印值的Go語法表示,即將生成該值的源代碼片段。
要打印值的類型,請使用 %T
。格式化布爾是比較直截了當的。有許多格式化整數的選項。對於標準的base-10
格式化,請使用%d
。
具體的每個函數,可參考示例中的代碼 -
所有的示例代碼,都放在
F:\worksp\golang
目錄下。安裝Go編程環境請參考:http://www.yiibai.com/go/go\_environment.html
string-formatting.go
的完整代碼如下所示 -
package main
import "fmt"
import "os"
type point struct {
x, y int
}
func main() {
// Go offers several printing "verbs" designed to
// format general Go values. For example, this prints
// an instance of our `point` struct.
p := point{1, 2}
fmt.Printf("%v\n", p)
// If the value is a struct, the `%+v` variant will
// include the struct's field names.
fmt.Printf("%+v\n", p)
// The `%#v` variant prints a Go syntax representation
// of the value, i.e. the source code snippet that
// would produce that value.
fmt.Printf("%#v\n", p)
// To print the type of a value, use `%T`.
fmt.Printf("%T\n", p)
// Formatting booleans is straight-forward.
fmt.Printf("%t\n", true)
// There are many options for formatting integers.
// Use `%d` for standard, base-10 formatting.
fmt.Printf("%d\n", 123)
// This prints a binary representation.
fmt.Printf("%b\n", 14)
// This prints the character corresponding to the
// given integer.
fmt.Printf("%c\n", 33)
// `%x` provides hex encoding.
fmt.Printf("%x\n", 456)
// There are also several formatting options for
// floats. For basic decimal formatting use `%f`.
fmt.Printf("%f\n", 78.9)
// `%e` and `%E` format the float in (slightly
// different versions of) scientific notation.
fmt.Printf("%e\n", 123400000.0)
fmt.Printf("%E\n", 123400000.0)
// For basic string printing use `%s`.
fmt.Printf("%s\n", "\"string\"")
// To double-quote strings as in Go source, use `%q`.
fmt.Printf("%q\n", "\"string\"")
// As with integers seen earlier, `%x` renders
// the string in base-16, with two output characters
// per byte of input.
fmt.Printf("%x\n", "hex this")
// To print a representation of a pointer, use `%p`.
fmt.Printf("%p\n", &p)
// When formatting numbers you will often want to
// control the width and precision of the resulting
// figure. To specify the width of an integer, use a
// number after the `%` in the verb. By default the
// result will be right-justified and padded with
// spaces.
fmt.Printf("|%6d|%6d|\n", 12, 345)
// You can also specify the width of printed floats,
// though usually you'll also want to restrict the
// decimal precision at the same time with the
// width.precision syntax.
fmt.Printf("|%6.2f|%6.2f|\n", 1.2, 3.45)
// To left-justify, use the `-` flag.
fmt.Printf("|%-6.2f|%-6.2f|\n", 1.2, 3.45)
// You may also want to control width when formatting
// strings, especially to ensure that they align in
// table-like output. For basic right-justified width.
fmt.Printf("|%6s|%6s|\n", "foo", "b")
// To left-justify use the `-` flag as with numbers.
fmt.Printf("|%-6s|%-6s|\n", "foo", "b")
// So far we've seen `Printf`, which prints the
// formatted string to `os.Stdout`. `Sprintf` formats
// and returns a string without printing it anywhere.
s := fmt.Sprintf("a %s", "string")
fmt.Println(s)
// You can format+print to `io.Writers` other than
// `os.Stdout` using `Fprintf`.
fmt.Fprintf(os.Stderr, "an %s\n", "error")
}
執行上面代碼,將得到以下輸出結果 -
F:\worksp\golang>go run string-formatting.go
{1 2}
{x:1 y:2}
main.point{x:1, y:2}
main.point
true
123
1110
!
1c8
78.900000
1.234000e+08
1.234000E+08
"string"
"\"string\""
6865782074686973
0xc042004280
| 12| 345|
| 1.20| 3.45|
|1.20 |3.45 |
| foo| b|
|foo |b |
a string
an error