ANi*_*sus 73 casting return-value go
在Go中转换多个返回值的惯用方法是什么?
你可以在一行中完成它,还是需要使用临时变量,例如我在下面的例子中做过的?
package main
import "fmt"
func oneRet() interface{} {
return "Hello"
}
func twoRet() (interface{}, error) {
return "Hejsan", nil
}
func main() {
// With one return value, you can simply do this
str1 := oneRet().(string)
fmt.Println("String 1: " + str1)
// It is not as easy with two return values
//str2, err := twoRet().(string) // Not possible
// Do I really have to use a temp variable instead?
temp, err := twoRet()
str2 := temp.(string)
fmt.Println("String 2: " + str2 )
if err != nil {
panic("unreachable")
}
}
Run Code Online (Sandbox Code Playgroud)
顺便说一句,它casting是否涉及到接口?
i := interface.(int)
Run Code Online (Sandbox Code Playgroud)
jim*_*imt 67
你不能一行完成.你的临时变量方法是可行的方法.
顺便说一句,在接口方面它被称为铸造吗?
它实际上称为类型断言.A型铸造转换不同的是:
var a int
var b int64
a = 5
b = int64(a)
Run Code Online (Sandbox Code Playgroud)
jor*_*lli 29
func silly() (interface{}, error) {
return "silly", nil
}
v, err := silly()
if err != nil {
// handle error
}
s, ok := v.(string)
if !ok {
// the assertion failed.
}
Run Code Online (Sandbox Code Playgroud)
但更有可能的是你真正想要的是使用类型开关,比如:
switch t := v.(type) {
case string:
// t is a string
case int :
// t is an int
default:
// t is some other type that we didn't name.
}
Run Code Online (Sandbox Code Playgroud)
Go更多的是关于正确性,而不是关于简洁性.
Mos*_*vah 12
template.Must是标准库在一个语句中仅返回第一个返回值的方法.可以针对您的情况进行类似的操作:
func must(v interface{}, err error) interface{} {
if err != nil {
panic(err)
}
return v
}
// Usage:
str2 := must(twoRet()).(string)
Run Code Online (Sandbox Code Playgroud)
通过使用must你基本上说永远不会有错误,如果有,那么程序不能(或至少不应该)继续运行,而是会发生恐慌.
或者只是在一个if:
if v, ok := value.(migrater); ok {
v.migrate()
}
Run Code Online (Sandbox Code Playgroud)
Go将处理if子句中的强制转换,并允许您访问已转换类型的属性.