You can not. The best you can do is write a helper function (and make a type statement in it):
func getDonut(p interface{}, err error) (Donut, error) {
return p.(Donut), err
}
And then it becomes single-line:
d, err := getDonut(getPastry())
Or you can even “include” the call getPastry()in a helper function:
func getDonutPastry() (Donut, error) {
p, err := getPastry()
return p.(Donut), err
}
( ):
d, err := getDonutPastry()
:
, , getPastry(), Donut, . , :
v, ok := x.(T)
. ok true, . false, v - T. .
( , ):
func getDonut2(p interface{}, err error) (Donut, error) {
if d, ok := p.(Donut); ok {
return d, err
} else {
return "", errors.New("Not a Donut!")
}
}
func getDonutPastry2() (Donut, error) {
p, err := getPastry()
if d, ok := p.(Donut); ok {
return d, err
} else {
return "", errors.New("Not a Donut!")
}
}
. :
, "ok" ,
Go: