gopl-zh.github.com/ch12/ch12-01.md

37 lines
2.0 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

## 12.1. 为何需要反射?
有时候我们需要编写一个函数能够处理一类并不满足普通公共接口的类型的值,也可能是因为它们并没有确定的表示方式,或者是在我们设计该函数的时候这些类型可能还不存在。
一个大家熟悉的例子是fmt.Fprintf函数提供的字符串格式化处理逻辑它可以用来对任意类型的值格式化并打印甚至支持用户自定义的类型。让我们也来尝试实现一个类似功能的函数。为了简单起见我们的函数只接收一个参数然后返回和fmt.Sprint类似的格式化后的字符串。我们实现的函数名也叫Sprint。
我们首先用switch类型分支来测试输入参数是否实现了String方法如果是的话就调用该方法。然后继续增加类型测试分支检查这个值的动态类型是否是string、int、bool等基础类型并在每种情况下执行相应的格式化操作。
```Go
func Sprint(x interface{}) string {
type stringer interface {
String() string
}
switch x := x.(type) {
case stringer:
return x.String()
case string:
return x
case int:
return strconv.Itoa(x)
// ...similar cases for int16, uint32, and so on...
case bool:
if x {
return "true"
}
return "false"
default:
// array, chan, func, map, pointer, slice, struct
return "???"
}
}
```
但是我们如何处理其它类似[]float64、map[string][]string等类型呢我们当然可以添加更多的测试分支但是这些组合类型的数目基本是无穷的。还有如何处理类似url.Values这样的具名类型呢即使类型分支可以识别出底层的基础类型是map[string][]string但是它并不匹配url.Values类型因为它们是两种不同的类型而且switch类型分支也不可能包含每个类似url.Values的类型这会导致对这些库的依赖。
没有办法来检查未知类型的表示方式,我们被卡住了。这就是我们需要反射的原因。