I am new to Flatbuffers and GoLang. I am trying to implement a function that take converts an object to flatbuffer and retrieves the same object. Here is my code. Updated Code
func getannouncements(){
annList := SR.GetFromDB().GetAllAnnouncementList()
fmt.Println(annList)
builder := flatbuffers.NewBuilder(1024)
var thisobjlist [12] flatbuffers.UOffsetT
for i,j := range annList{
annTitle := builder.CreateString(j.AnnTitle)
annText := builder.CreateString(j.AnnText)
annDate := builder.CreateString(j.AnnDate)
fb.AnnouncementStart(builder)
fb.AnnouncementAddAnnId(builder,int32(j.AnnID))
fb.AnnouncementAddAnnTitle(builder,annTitle)
fb.AnnouncementAddAnnText(builder, annText)
fb.AnnouncementAddAnnActive(builder,CR.BoolToByte(j.AnnActive))
fb.AnnouncementAddAnnDate(builder,annDate)
thisobj:= fb.AnnouncementEnd(builder)
thisobjlist[i] = thisobj
}
fb.AnnouncementListStartAnnListVector(builder,len(annList))
for _,j:=range thisobjlist{
builder.PlaceUOffsetT(j)
}
finalObj := fb.AnnouncementListEnd(builder)
builder.Finish(finalObj)
buf:= builder.FinishedBytes()
fmt.Println(buf)
/*bufItem := new(bytes.Buffer)
binary.Write(bufItem, binary.LittleEndian, buf)
buf1 := bufItem.Bytes()
buffyRead := bytes.NewReader(buf1)
var buffy []byte
binary.Read(buffyRead, binary.LittleEndian, &buffy)*/
Anncmt:= fb.GetRootAsAnnouncementList(buf,0)
anns := new(fb.Announcement)
if Anncmt.AnnList(anns,1){
thisLists := anns.AnnTitle()
fmt.Println(thisLists)
}
fmt.Println(Anncmt)
}
Schema File
namespace FlatBufs;
table Announcement{
AnnId:int;
AnnTitle:string;
AnnText:string;
AnnDate:string;
AnnActive:bool= false;
}
table AnnouncementList{
AnnName:string;
AnnList:[Announcement];
}
root_type AnnouncementList
The buf object is a byte array. However when I am generating the AnnGot obj I am still getting the almost same byte array as buf. So, When I read various posts on internet on this topic, I tried to convert that buf to binary type and then retrieve buf and tried to retrieve the data (as in the commented part of code). This time the object buffy doesn't have any data in it. I am still not clear what mistake I am making in this entire code.
Please point me in the right direction. Any help on this is much appreciated.
Thanks, Tom