例如,我有这段代码:
type MyStruct struct {
...
MyField *MyOtherStruct
...
}
type MyOtherStruct struct {
...
MyOtherField *string
...
}
// I have a fuction that receive MyOtherField as parameter
func MyFunc(myOtherField *string) {
...
}
// How can I avoid using if/else here
if MyStruct.MyField != nil {
MyFunc((*MyStruct.MyField).MyOtherField)
} else {
MyFunc(nil)
}
在我的示例中,我必须使用If Else来处理MyStruct.MyField
是否为零.我想找到一种方法来缩短我的代码.
我想找一些像MyFunc(MyStruct.MyField ? (*MyStruct.MyField).MyOtherField : nil)
在JavaScript中的方法.