I'm very new to Go programming (3-4 days), and I'm trying to write some code that reads a binary file using an existing third-party C library using cgo. The C library's way of doing this seems fairly standard (for C). Slightly simplified it looks like:
int main(int argc, char *argv[]) {
file_t *file = file_open(filename);
index_t *index = index_load(file, filename);
iterator_t *iter = query(idx, header, region);
record_t *record = record_init();
while (iterator_next(file, iter, record) >= 0) {
/* Do stuff with record */
}
iterator_destroy(iter);
record_destroy(record);
file_close(file);
return 0;
}
I have written the following Go code:
func main() {
file := Open(filename)
record := NewRecord()
iter := file.Query(region)
for {
n, err := file.Next(iter, record)
if err != nil {
log.Fatal(err)
}
if n <= 0 {
// No more records to read.
break
}
}
}
This works, in the sense that it will allow me to access the records in a particular query region.
My question is whether this is an idiomatic way to approach this task in Go, or are there better alternatives? I've seen sites such as http://ewencp.org/blog/golang-iterators, but seem unable to get these examples to work with the C library (I was thinking it may be because the C library is reusing the record_t variable on each iteration rather than creating a new variable, but maybe it's just my lack of experience with Go).