I have this struct defined in some file:
type dbHypervisors struct {
HID uint8 `json:"hid" yaml:"hid"`
Hname string `json:"hname" yaml:"hname"`
Haddress string `json:"haddress" yaml:"address"`
}
And I use it this way, later on:
func getHypervisorData(conn *pgx.Conn) []dbHypervisors {
var hyps []dbHypervisors
rows, err := conn.Query(context.Background(), "SELECT hID, hName, hAddress from config.hypervisors")
if err != nil {
fmt.Println("Error: ", err)
}
defer rows.Close()
<SOME PROCESSING>
return hyps
}
Now, once the processing is done, I’d wish to serialize the struct in JSON (the code for this works, so the issue is not there. I wanted to implement it through a function type receiver such as:
func (h dbHypervisors) serialize(filename string) {
//fmt.Printf("%T \n", jsonObject)
}
and from my main function, I’ do something like:
hypervisors:= getHypervisorData(conn)
hypervisors.serialize(targetFile)
It complains that serialize() is not resolving. How would I manage to use the function receiver properly ? I guess it expects a dbHypervisor type while I’d wish to use a slice, there.
I want serialize to truncate the file and write everything all at once. I have to go through a for … range loop, it means I’d have to change the logic in serialize() to append each element of the slice, and I want to avoid this.
Any idea ?