I'm trying to parse log strings, where fields are glued with "|" char. All logs suppose to have 20 fields, but some logs have less. I don't want to discard them, but rather fill Log{}
with whatever info was there in the log string.
type Log struct {
Field1 string
Field2 uint64
Field3 string
// ...
Field20 string
}
Knowing that it's not possible to check if index exists within a slice, this looks suboptimal:
log := Log{}
c := len(fields)
if c > 0 {
log.Field1 = fields[0]
}
if c > 1 {
log.Field2, _ = strconv.ParseUint(fields[1], 0, 64)
}
if c > 3 {
log.Field3 = fields[2]
}
//...
Any better ways doing this?
Thanks