One possible solution is the "encoding/gob" standard package. The gob package creates an encoder/decoder that can encode any struct into an array of bytes and then decode that array back into a struct. There's a great post, here.

As others have pointed out, it's necessary to use a package like this because structs, by their nature, have unknown sizes and cannot be converted into arrays of bytes.

I've included some code and a play.

package main

import (
    "bytes"
    "encoding/gob"
    "fmt"
    "log"
)

type P struct {
    X, Y, Z int
    Name    string
}

type Q struct {
    X, Y *int32
    Name string
}

func main() {
    // Initialize the encoder and decoder.  Normally enc and dec would be
    // bound to network connections and the encoder and decoder would
    // run in different processes.
    var network bytes.Buffer        // Stand-in for a network connection
    enc := gob.NewEncoder(&network) // Will write to network.
    dec := gob.NewDecoder(&network) // Will read from network.
    // Encode (send) the value.
    err := enc.Encode(P{3, 4, 5, "Pythagoras"})
    if err != nil {
        log.Fatal("encode error:", err)
    }

    // HERE ARE YOUR BYTES!!!!
    fmt.Println(network.Bytes())

    // Decode (receive) the value.
    var q Q
    err = dec.Decode(&q)
    if err != nil {
        log.Fatal("decode error:", err)
    }
    fmt.Printf("%q: {%d,%d}\n", q.Name, *q.X, *q.Y)
}
Answer from Adam on Stack Overflow
Top answer
1 of 11
51

One possible solution is the "encoding/gob" standard package. The gob package creates an encoder/decoder that can encode any struct into an array of bytes and then decode that array back into a struct. There's a great post, here.

As others have pointed out, it's necessary to use a package like this because structs, by their nature, have unknown sizes and cannot be converted into arrays of bytes.

I've included some code and a play.

package main

import (
    "bytes"
    "encoding/gob"
    "fmt"
    "log"
)

type P struct {
    X, Y, Z int
    Name    string
}

type Q struct {
    X, Y *int32
    Name string
}

func main() {
    // Initialize the encoder and decoder.  Normally enc and dec would be
    // bound to network connections and the encoder and decoder would
    // run in different processes.
    var network bytes.Buffer        // Stand-in for a network connection
    enc := gob.NewEncoder(&network) // Will write to network.
    dec := gob.NewDecoder(&network) // Will read from network.
    // Encode (send) the value.
    err := enc.Encode(P{3, 4, 5, "Pythagoras"})
    if err != nil {
        log.Fatal("encode error:", err)
    }

    // HERE ARE YOUR BYTES!!!!
    fmt.Println(network.Bytes())

    // Decode (receive) the value.
    var q Q
    err = dec.Decode(&q)
    if err != nil {
        log.Fatal("decode error:", err)
    }
    fmt.Printf("%q: {%d,%d}\n", q.Name, *q.X, *q.Y)
}
2 of 11
42

I assume you want something like the way C handles this. There is no built in way to do that. You will have to define your own serialization and deserialization to and from bytes for your struct. The binary package will help you encode the fields in your struct to bytes that you can add to the byte array but you will be responsible for specifying the lengths and offsets in the byte array that will hold the fields from your struct.

Your other options are to use one of the encoding packages: http://golang.org/pkg/encoding/ such as gob or json.

EDIT:

Since you want this for making a hash as you say in your comment the easisest thing to do is use []byte(fmt.Sprintf("%v", struct)) like so: http://play.golang.org/p/yY8mSdZ_kf

🌐
Google Groups
groups.google.com › g › golang-nuts › c › NF-SfDi5jhE
golang convert a struct to a byte array
March 31, 2014 - Finally, the possible solution is convert the struct to json and generate the binary but i´m sure that is not the best solution (efficient) ... Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message ... On Mon, Mar 31, 2014 at 08:39:20AM -0700, Juan Torres wrote: > Hello, i´m trying to use the package kv (https://github.com/cznic/kv) and > the method Set and the param is a array byte > > func (db *DB) Set(key, value []byte) (err error) { > ...
🌐
Reddit
reddit.com › r/golang › the *right* way to do a bytesstruct in golang
r/golang on Reddit: The *right* way to do a bytes<->struct in Golang
October 10, 2024 -

In C, for a protocol stack, I'd have a byte array from the network. I'd inspect a couple of bytes to determine what the packet is supposed to be at least, and then cast it to the appropriate struct. Something like this:

bptr = <malloc'd buffer>
*protocolStruct = *(ProtocolObject)bptr

In golang, do I use the binary package, the unsafe { }, or what?

Let''s assume I have a struct for an ARP request, something like:

type NativeAddress struct
      AddressType uint32
      Address [16]byte
}

And my ARP request structure is

type ARPRequest struct {
     SAP uint16
     NSAP uint16
     SrcAddress NativeAddres
     DstAddress NativeAddress
     RequestedAddress uint64
}

How does this get encoded with something like Gob and how does it get received on the other end? Wasteful though it is, everything is aligned. Can I just assume it gets encoded linearly ion the wire? In ancient C, I could do a tagged union that worked the same way, but modern C doesn't do that, you really can't count on what two C compilers will generate for structs <-> bytes. Or am I making this harder than it has to be -- should I just encode everything, every packet in protobuf, avro etc? In JVM langues, I have the same problem so I was hoping Golang handled it better. Even with Gob, can two architectures using Golang encode and decode structs reliably and, I still have the problem, after I read the SAP and NSAP bytes, only then do I know what struct it actually is and thus, I can "cast" it, or does Gob take care of that? I can't use something like JSON encoding, this is binary date.

I'd love to just have something that read bytes and said "I just received struct X" or "I have no idea what I just got" if I can't do the C style tricks.

🌐
Medium
medium.com › @maximgradan › serialize-a-struct-to-bytes-to-send-it-through-the-network-in-go-part-i-b861c15d2f06
Serialize a struct to bytes to send it through the network in Go — Part I | by Maxim Gradan | Medium
June 2, 2022 - The pointer is just a number, an ... the unsafe cast actually looks: Here we just take an address of the struct and interpret it as a byte array pointer then turn it into a slice....
🌐
GitHub
gist.github.com › amlwwalker › 92d60ae8b27099c166dd013161052c4f
Taking a struct, converting it to a byte array, gob encoding that, compressing it, decompressing it and converting back to a byte array/struct · GitHub
Taking a struct, converting it to a byte array, gob encoding that, compressing it, decompressing it and converting back to a byte array/struct - compressor.go
🌐
Go Forum
forum.golangbridge.org › technical discussion
C style struct to data (byte array) mapping, but in Go - Technical Discussion - Go Forum
June 10, 2016 - So in Go I have this struct: type msgHeader struct { format byte length [2]byte //BCD-4 msgType byte } Then I have this byte array and variable: var data [4]byte{0x46, 0x00, 0x04, 0x45} var hdr msgHeade…
Find elsewhere
🌐
Reddit
reddit.com › r/golang › create repeatable byte array of go struct which contains a pointer
r/golang on Reddit: Create repeatable byte array of Go struct which contains a pointer
May 20, 2020 -

I want to be able to create repeatable byte arrays of structs in Go so I can hash them and then verify that hash at some point.

I am currently following this simple approach to create a byte array from a struct with:

    []byte(fmt.Sprintf("%v", struct))...)

This works perfectly until my struct holds an embedded struct with a pointer, for example:

    type testEmbeddedPointerStruct struct {
    	T *testSimpleStruct
    }

In my tests this creates a different byte array each time, I think it may be because with the pointer the address in memory changes each time?

Is there a way of creating a repeatable byte array digest even if the struct holds a pointer?

I basically want to get the values from the struct and convert them to a byte array

Thanks

🌐
Go
go.dev › play › p › vUUboD7O_T
Go Playground - The Go Programming Language
Common problems companies solve with Go · Stories about how and why companies use Go
🌐
php.cn
m.php.cn › home › backend development › golang › convert struct to byte golang
Convert struct to byte golang-Golang-php.cn
In In the above code, we use the binary.Write function to convert the structure User into a byte array, where the buf variable is the buffer to be written.
🌐
GitHub
gist.github.com › SteveBate › 042960baa7a4795c3565
Example of converting a struct to a byte slice, compressing with gzip, and saving to file followed by the reverse process back to a struct · GitHub
Example of converting a struct to a byte slice, compressing with gzip, and saving to file followed by the reverse process back to a struct - struct_to_bytes.go
🌐
Google Groups
groups.google.com › g › golang-nuts › c › nEGhrzwj94I
Casting []byte to data struct
to golang-nuts · Hello, I am trying to read registers from a file, but os.Read() require a [] byte parameter, and my data structure is similar to: type Data struct { id uint64; name [100]byte; telephone [100]byte; } How can I cast []byte to Data? This is not valid: var bs [100]byte; var d *Data = &bs; Error: cannot use &bs (type *[100]uint8) as type *Data Any idea?
🌐
CodeGenes
codegenes.net › blog › in-go-how-can-i-convert-a-struct-to-a-byte-array
How to Convert a Struct to a Byte Array in Go: Step-by-Step Guide with Examples — codegenes.net
Byte arrays are lightweight, compact, and easy to transmit or store, making them ideal for these scenarios. This guide will walk you through four methods to convert a struct to a byte array in Go, including low-level binary encoding, Go-specific serialization, text-based formats, and cross-language protocols.
🌐
gosamples
gosamples.dev › tutorials › convert a struct to io. reader in go
📔 Convert a struct to io.Reader in Go
December 1, 2021 - Alternatively, you can encode the struct into a JSON byte slice using the json.Marshal() function and create a new bytes.Reader that is a struct for reading data from this slice which implements the required io.Reader interface.
🌐
Grokbase
grokbase.com › t › gg › golang-nuts › 143zqky8g5 › go-nuts-golang-convert-a-struct-to-a-byte-array
[go-nuts] golang convert a struct to a byte array - Grokbase
March 31, 2014 - To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com. For more options, visit https://groups.google.com/d/optout. ... On Mon, Mar 31, 2014 at 08:39:20AM -0700, Juan Torres wrote: Hello, i´m trying to use the package kv (https://github.com/cznic/kv) and the method Set and the param is a array byte func (db *DB) Set(key, value []byte) (err error) { ... } and i would like to convert one structure to array byte..Given the data store, I assume your program is the only consumer of the encoded values.
🌐
Programmersought
programmersought.com › article › 15932059401
Golang transfer byte array structure / golang struct to byte / golang byte aligned memory - Programmer Sought
Recent golang do serial communication, using the clang very simple to implement, use golang you need to use some powerful library. Simple structure transfer byte array, using the json, gob very simple to implement, use the following example to write binary library