mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2025-08-06 23:41:43 +00:00
ch7: fix code format
This commit is contained in:
@@ -2,8 +2,8 @@
|
||||
|
||||
在本章,我們會學到另一個標準的接口類型flag.Value是怎麽幫助命令行標記定義新的符號的。思考下面這個會休眠特定時間的程序:
|
||||
|
||||
<u></i>gopl.io/ch7/sleep</i></u>
|
||||
```go
|
||||
// gopl.io/ch7/sleep
|
||||
var period = flag.Duration("period", 1*time.Second, "sleep period")
|
||||
|
||||
func main() {
|
||||
@@ -51,8 +51,8 @@ String方法格式化標記的值用在命令行幫組消息中;這樣每一
|
||||
|
||||
讓我們定義一個允許通過攝氏度或者華氏溫度變換的形式指定溫度的celsiusFlag類型。註意celsiusFlag內嵌了一個Celsius類型(§2.5),因此不用實現本身就已經有String方法了。爲了實現flag.Value,我們隻需要定義Set方法:
|
||||
|
||||
<u><i>gopl.io/ch7/tempconv</i></u>
|
||||
```go
|
||||
// gopl.io/ch7/tempconv
|
||||
// *celsiusFlag satisfies the flag.Value interface.
|
||||
type celsiusFlag struct{ Celsius }
|
||||
|
||||
@@ -89,8 +89,8 @@ func CelsiusFlag(name string, value Celsius, usage string) *Celsius {
|
||||
|
||||
現在我們可以開始在我們的程序中使用新的標記:
|
||||
|
||||
<u><i>gopl.io/ch7/tempflag</i></u>
|
||||
```go
|
||||
// gopl.io/ch7/tempflag
|
||||
var temp = tempconv.CelsiusFlag("temp", 20.0, "the temperature")
|
||||
|
||||
func main() {
|
||||
|
Reference in New Issue
Block a user