ch7/ch7-04 #151
Replies: 5 comments
-
本节练习 - 新冠阳了,难受的一批发烧41度多, 卷不动了, 等好了在看吧.. type Celsius float64
type Fahrenheit float64
type Kelvin float64
func CToF(c Celsius) Fahrenheit { return Fahrenheit(c*9.0/5.0 + 32.0) }
func FToC(f Fahrenheit) Celsius { return Celsius((f - 32.0) * 5.0 / 9.0) }
func KToC(k Kelvin) Celsius { return Celsius(k - 273.15) }
// 练习7.7 为啥默认值是带有°C的字符串
// 因为Celsius类型实现了String()方法,
// 在flag.CommandLine.Var(&f, name, usage)时,
// 会调用f.String()方法,给Flag结构体的DefValue属性就是带有°C这个符号的
// 而default的这句话是在flag.Parse()调用其他函数中执行输出的
// 见Flag.FlagSet.defaultUsage() 和 PrintDefaults()方法
func (c Celsius) String() string { return fmt.Sprintf("%gC", c) }
// *celsiusFlag satisfies the flag.Value interface.
type celsiusFlag struct{ Celsius }
func (f *celsiusFlag) Set(s string) error {
var unit string
var value float64
fmt.Sscanf(s, "%f%s", &value, &unit) // no error check needed
switch unit {
case "C", "°C":
f.Celsius = Celsius(value)
return nil
case "F", "°F":
f.Celsius = FToC(Fahrenheit(value))
return nil
case "K", "°K":
f.Celsius = KToC(Kelvin(value))
return nil
}
return fmt.Errorf("invalid temperature %q", s)
}
// CelsiusFlag defines a Celsius flag with the specified name,
// default value, and usage, and returns the address of the flag variable.
// The flag argument must have a quantity and a unit, e.g., "100C".
func CelsiusFlag(name string, value Celsius, usage string) *Celsius {
f := celsiusFlag{value}
flag.CommandLine.Var(&f, name, usage)
return &f.Celsius
}
var temp = CelsiusFlag("temp", 20.0, "the temperature")
func main() {
flag.Parse()
fmt.Println(*temp)
} |
Beta Was this translation helpful? Give feedback.
-
越看越懵逼 |
Beta Was this translation helpful? Give feedback.
-
7.5func CelsiusFlag(name string, value Celsius, usage string) *Celsius {
f := celsiusFlag{value} // 这里默认就是使用 Celsius 类型初始化,String 方法也是使用的 Celsius 类型的
......
} |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
完全正确。Go语言中,如果一个类型通过内嵌(embedding)的方式包含了另一个类型,而这个内嵌的类型已经实现了某个接口所需的方法,那么这些方法会被“提升”到外层类型的作用域中。也就是说,外层类型也自动拥有了这些方法,因此能够满足接口的定义要求,可以作为该接口来使用。 比如,在你的例子中,celsiusFlag内嵌了一个Celsius类型,假设Celsius已经实现了String()方法,那么这个方法就会自动成为celsiusFlag的一部分。再加上celsiusFlag自定义实现了Set(string) error方法,*celsiusFlag自然就满足了flag.Value接口的所有要求。这就使得它可以被当做一个实现了flag.Value接口的类型来调用。 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
ch7/ch7-04
中文版
https://golang-china.github.io/gopl-zh/ch7/ch7-04.html
Beta Was this translation helpful? Give feedback.
All reactions