I have a file with many types of data record which I need to parse into structs.
I'd be grateful to learn of a idiomatic way -- if it exists -- of filling structs by record type. Something like python's namedtuple(*fields)
constructor.
package main
import (
"fmt"
"strconv"
"strings"
)
type X interface{}
type HDR struct {
typer, a string
b int
}
type BDY struct {
typer, c string
d int
e string
}
var lines string = `HDR~two~5
BDY~four~6~five`
func sn(s string) int {
i, _ := strconv.Atoi(s)
return i
}
func main() {
sl := strings.Split(lines, "\n")
for _, l := range sl {
fields := strings.Split(l, "~")
var r X
switch fields[0] {
case "HDR":
r = HDR{fields[0], fields[1], sn(fields[2])} // 1
case "BDY":
r = BDY{fields[0], fields[1], sn(fields[2]), fields[3]} // 2
}
fmt.Printf("%T : %v\n", r, r)
}
}
I'm specifically interested to learn if lines marked // 1
and // 2
can be conveniently replaced by code, perhaps some sort of generic decoder which allows the struct itself to handle type conversion.