我们可以做这样的地图:
value, present := m["key"]
要么
value := m["key"]
并键入断言
var i interface{} = "hello"
s := i.(string)
fmt.Println(s)
s, ok := i.(string)
fmt.Println(s, ok)
Run Code Online (Sandbox Code Playgroud)
但我找不到一种方法来定义一个可以返回1值或2值的函数.例如:
func hello() (string, error) {
return "world", nil
}
Run Code Online (Sandbox Code Playgroud)
调用func时:
v, ok := hello() // valid
v := hello() // invalid
Run Code Online (Sandbox Code Playgroud)
PS:我知道类似template.Must但似乎有所不同,我真的想知道Map和类型断言如何能够做到这一点.
提前致谢.(我清楚了吗?可怜的英语对不起.