Say i am expecting this list from a query to a backend:
Name: Volvo, EngineType: 2NR-FE, Warranty: 2 years, Distance covered: 9000km.
In the case e.g EngineType is empty which equals to a nil, I want to set that to "None" or if Warranty == nil i want to set that to "No warranty found"
Here is my loop:
for _, res := range resp.Automobiles {
if res.Cars != nil {
for _, car := range res.Cars {
if car == nil || car.EngineType == nil {
fmt.Println("None")
} else if car.Warranty == nil {
fmt.Println("NO Warranty Found")
} else {
details = []string{*car.Name, *car.EngineType, *car.Warranty, *car.DistanceCovered}
fmt.Println(strings.Join(details, ","))
}
}
}
}
}
Output I'm expecting if Warranty == nil and EngineType == nil respectively:
Volvo, 2NR-FE, No warranty found, 9000km
Volvo, None, 2 years, 9000km
Output I'm getting:
No warranty Found
None
My code just sets the line to the first expression it checks
I know my code might be shitty, I'm still getting used to Go.