Idiomatic way to do conversion/type assertion on multiple return values in Go

后端 未结 4 650
花落未央
花落未央 2021-01-30 08:12

What is the idiomatic way to cast multiple return values in Go?

Can you do it in a single line, or do you need to use temporary variables such as I\'ve done in my exampl

4条回答
  •  自闭症患者
    2021-01-30 08:57

    Or just in a single if:

    if v, ok := value.(migrater); ok {
        v.migrate()
    }
    

    Go will take care of the cast inside the if clause and let you access the properties of the casted type.

提交回复
热议问题