4 ms·
> if (data.name != "") { > return data.name > } else { > return "New Data" > } Not exactly... if data.name != "" { return
by sagichmal 12y ago
> if (data.name != "") {
> return data.name
> } else {
> return "New Data"
> }
Not exactly...
if data.name != "" {
return data.name
}
return "New Data"
And consider the likely context...
func name(ref int64) string {
data, ok := get(ref)
if !ok {
return "No Data"
}
if !data.valid {
return "Invalid Data"
}
if data.name == "" {
return "New Data"
}
return data.name
}
Straightforward and unambiguous.
- vorg 12y agoDoesn't compile though. You'll need lots of extra lines just to see an output, which is really much of what the posted article was talking about with its wandering-eyed gopher tale... package main import "fmt" func main(){ fmt.Println(name(28)) } type T struct{name string; valid bool} func get(ref int64) (T, bool) { return T{"abc", true}, false } func name(ref int64) string { switch data, ok := get(ref); true { case !ok: return "No Data" case !data.valid: return "Invalid Data" case data.name == "": return "New Data" default: return data.name } }