What is the idiomatic way to apply multiple return values ββin Go?
Can you do this on one line or do you need to use temporary variables, for example, what I did in my example below?
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") } }
By the way, is this called casting when it comes to interfaces?
i := interface.(int)
casting go return-value
ANisus Jul 09 2018-12-12T00: 00Z
source share