Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to ignore an error return value in Golang? [duplicate]

Tags:

go

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.

like image 293
anonrose Avatar asked Mar 15 '16 18:03

anonrose


1 Answers

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.

like image 188
evanmcdonnal Avatar answered Oct 22 '22 04:10

evanmcdonnal