I'm trying same my nested struct to binary files. In future there will be a lots of "Rooms" records so serialized struct in binary file is the best approach I think.
package main
import (
"bytes"
"encoding/binary"
"log"
"time"
)
type House struct {
ID int
Floors int
Rooms []Room
}
type Room struct {
Width int
Height int
Description string
CreatedAt time.Time
}
func main() {
var house House = House{
ID: 1,
Floors: 3,
}
house.Rooms = append(house.Rooms, Room{Width: 20, Height: 30, CreatedAt: time.Now(), Description: "This is description"})
house.Rooms = append(house.Rooms, Room{Width: 14, Height: 21, CreatedAt: time.Now(), Description: "This is other description"})
house.Rooms = append(house.Rooms, Room{Width: 12, Height: 43, CreatedAt: time.Now(), Description: "This is other desc"})
log.Println(house)
buf := new(bytes.Buffer)
err := binary.Write(buf, binary.LittleEndian, house)
if err != nil {
log.Println(err)
}
}
But I have error: - Binary.Write: invalid type main.House
Could someone help me because I cant find solution.