At the moment I am using the AWS sdk with golang. Most of the API commands return deep structs and all fields of this struct(s) are pointers to be able to represent the absence of data.
But this leads to very ugly code since if you code devensively and want to grab a value deep down the tree this may look like this
func getInstanceID(res *Result) string {
if result == nil ||
len(result.reservations) == 0 ||
result.reservations[0].instance == nil ||
result.reservations[0].instance.instanceID == nil {
return ""
}
return *result.reservations[0].instance.instanceID
}
This is where for example Kotlin safe call feature would come handy.
Could the following code be considered best practice (or rather bad practice) in go:
func getInstanceID(res *Result) string {
defer func() { recover() }()
return *result.reservations[0].instance.instanceID
}
So you don't have to take care of all the nil and length checks, and if something fails the zero value is returned.
What do you think?