I'm trying to initialize a struct in Go, one of my values is being which returns both an int and an error if one was encountered converted from a string using strconv.Atoi("val")
.
My question is : Is there a way to ignore an error return value in Golang?
ts := &student{
name: td[0],
ssn: td[2],
next: students.next,
age: strconv.Atoi(td[1]),
}
which gives the error
multiple-value strconv.Atoi() in single-value context
if I add in the err, which i don't want to include in my struct, I will get an error that I am using a method that is not defined in the struct.
You can ignore a return value using _
on the left hand side of assignment however, I don't think there is any way to do it while using the 'composite literal' initialization style you have in your example.
IE I can do returnValue1, _ := SomeFuncThatReturnsAresultAndAnError()
but if you tried that in your example like;
ts := &student{
name: td[0],
ssn: td[2],
next: students.next,
age, _: strconv.Atoi(td[1]),
}
It will also generate a compiler error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With