how to Read and Write a Structure to a File in Binary Mode

6,380 views
Skip to first unread message

Jian Zhou

unread,
Mar 29, 2012, 1:46:39 PM3/29/12
to golan...@googlegroups.com
Anyone can help me about how to read and write a structure in binary mode like c/c++?

I tried but failed.

minux

unread,
Mar 29, 2012, 1:49:13 PM3/29/12
to Jian Zhou, golan...@googlegroups.com
golang.org/pkg/encoding/binary, but beware the alignment issues.

Stephen Weinberg

unread,
Mar 29, 2012, 1:57:44 PM3/29/12
to golang-nuts
You need to somehow marshal/encode the structure. A good, easy, no
work solution is to use http://golang.org/pkg/encoding/gob/. You can
also use encoding/binary to turn ints into bytes and marshal manually.

-- Stephen

Jian Zhou

unread,
Mar 29, 2012, 2:00:42 PM3/29/12
to golan...@googlegroups.com, Jian Zhou
Thanks!

This code works.

package main

import (
        "fmt"
        "bytes"
        "encoding/binary"
)

func main() {
    buf := new(bytes.Buffer)
    var data = []interface{}{
        uint16(61374),
        int8(-54),
        uint8(254),
    }
    for _, v := range data {
        err := binary.Write(buf, binary.LittleEndian, v)
        if err != nil {
            fmt.Println("binary.Write failed:", err)
        }
    }
    fmt.Printf("%x", buf.Bytes())

Jian Zhou

unread,
Mar 29, 2012, 2:06:07 PM3/29/12
to golan...@googlegroups.com
Thanks!

Does go support pointer types conversion? That would be more easy. I've tried many times..

Maybe I'm still think in c...

minux

unread,
Mar 29, 2012, 2:17:22 PM3/29/12
to Jian Zhou, golan...@googlegroups.com
On Fri, Mar 30, 2012 at 2:06 AM, Jian Zhou <zhou...@tiaozhanshu.net> wrote:
Does go support pointer types conversion? That would be more easy. I've tried many times..
It does support it, but use this feature isn't portable and quite dangerous (don't use it!).
like this (cast *uint to *byte):
package main
import ("unsafe"; "fmt")
func main() {
var a uint = 0xbeefcafe
fmt.Println(*(*byte)(unsafe.Pointer(&a)))
}

steve wang

unread,
May 3, 2013, 11:00:13 PM5/3/13
to golan...@googlegroups.com
Could you elaborate on "alignment issues"? 

John Beisley

unread,
May 4, 2013, 3:18:44 AM5/4/13
to steve wang, golang-nuts

encoding/binary writes Go structs packed - that is: without any padding to align members to word boundaries (which don't make sense for a serialised format). In contrast, if someone were to write the raw in-memory bytes of a struct to a serialised format, then they would find padding bytes are present.

(I think that's what was meant by 'alignment issues')

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

steve wang

unread,
May 4, 2013, 6:30:41 AM5/4/13
to golan...@googlegroups.com
Thanks. I mistook what he meant.
I thought he meant that alignment should be made explicitly when using encoding/binary.
Reply all
Reply to author
Forward
0 new messages