0

Is there any way in Go to do this:

segment := Segment{
    CumulativeDistanceMm:    strconv.Atoi(record[9]),
    Length:                  strconv.Atoi(record[1]),
    LinkId:                  strconv.Atoi(record[8]),
    SegmentId:               strconv.Atoi(record[2]),
}

The error that I get is that strconv.Atoi returns multiple values so I can't assign it directly to the struct properties. If it was a variable I could use the underscore to ignore the second value. Can I do something similar for structs?

Aetherix
  • 2,150
  • 3
  • 24
  • 44

1 Answers1

0

strconv.Atoi can fail and you have to deal with this failure. If such failures are absolutely impossible you would write a function func MustAtoi(s string) int which panics on failure and use that one in your struct initialization.

In Go doing some programming instead of using syntactical sugar or fancy syntax is common.

Most probably you should rethink your error handling.

Volker
  • 40,468
  • 7
  • 81
  • 87