0

I'm starting to learn golang but come across what I hope is a simple problem.

I have a file written to in C with several structs. ie myStruct's Now I want to read one struct of data from this file.

In C I simple open a file and move the fileptr number of structs * sizeofStruct. Like this

int sizeofStruct = sizeof(myStruct)
seek(filehandle, searchNo*sizeofStruct)
read(filehandle, &data, sizeofStruct)

This doesn't seem to be as simple in Golang as "sizeof"... rather multiple conversions ending up in uintptr... something, or reflect.int32()

var spect Spectrum // struct Spectrum
const SizeOfSpectrum = unsafe.Sizeof(spect)

I was hoping SizeOfSpectrum would contain equal to sizeof(spect) in C

Can you guys help me to get the size of a struct in an int variable?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Anders S
  • 187
  • 11
  • 6
    See possible duplicate of [How to get memory size of variable in golang?](https://stackoverflow.com/questions/44257522/how-to-get-memory-size-of-variable-in-golang/44258164#44258164) – icza Feb 25 '19 at 15:45
  • 1
    Binary data is usually read with the [encoding/binary package](https://golang.org/pkg/encoding/binary/) in Go. If that is an option you don't have to deal with struct sizes; the package handles that automatically. – Peter Feb 25 '19 at 16:15
  • 2
    How is the `Spectrum` Go `struct` defined? – peterSO Feb 25 '19 at 16:22
  • @peterSO, the struct is taken from the C-definition straight and first defined as type INT int16, type CHAR byte. Then made the Spectrum struct like this type Spectrums struct { // char verkid[30]; verkID [30]CHAR // int streamnr streamNr INT // int cellinfo; cellInfo INT .... a.s.o. } – Anders S Feb 26 '19 at 08:02
  • @icza thanks, They speak of traversing through the struct first and returning the size of the actual values, ? meaning the values from file or empty struct?? Or do I miss something here?... I'll give it a try to see – Anders S Feb 26 '19 at 08:14
  • @Peter Yes that is true, however I want to read one specific struct from the file at specific position ie noOfStruct * datasize and extract the data – Anders S Feb 26 '19 at 08:16
  • The only issue left with this, after testing, unsafe.Sizeof(struct) give me the correct size as a uintptr. I canprint the value with like fmt.Print(unsafe.Sizeof(struct)) but I haven't found a way to get the numeric value and multiply with another, say int16 value. Purpose is to seek a position within the file and use ReadAt(receiverAsByteArray, intFromWhatPosition) Or later on, to write struct on same position – Anders S Feb 26 '19 at 11:36

1 Answers1

1

I have a file written to in C with several structs. ie myStruct's Now I want to read one struct of data from this file. In C i simple open a file and move the fileptr number of structs * sizeofStruct.

I haven't found a way to get the numeric value and multiply with another, say int16 value. Purpose is to seek a position within the file.


You do the same thing in Go, using explicit conversions.

For example,

package main

import (
    "fmt"
    "unsafe"
)

func main() {
    type S struct{ F int32 }
    i16 := int16(42)
    // The Go Programming Language Specification
    // https://golang.org/ref/spec
    // Numeric types
    // Explicit conversions are required when different
    // numeric types are mixed in an expression or assignment.
    // func (f *File) Seek(offset int64, whence int) (ret int64, err error)
    offset := int64(i16) * int64(unsafe.Sizeof(S{}))
    fmt.Println(offset)
}

Playground: https://play.golang.org/p/YFyU11Lf2qc

Output:

168
peterSO
  • 158,998
  • 31
  • 281
  • 276