golang convert a struct to a byte array

21,360 views
Skip to first unread message

Juan Torres

unread,
Mar 31, 2014, 11:11:37 AM3/31/14
to golan...@googlegroups.com
I´m try to convert a struct to a byte array, but i´m getting a error and i´m no sure it´s the best way,
can help me?

package main

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

//My Struct
type Number struct {
    One       string
    Two     string
}


func main() {
buf := new(bytes.Buffer)

var number Number
key := "1"
number.One = key
number.Two = "2"

err := binary.Write(buf, binary.BigEndian, &number)

if err != nil {
fmt.Println("binary.Write failed:", err)
}
}

Error result :     binary.Write failed: binary.Write: invalid type string

Jan Mercl

unread,
Mar 31, 2014, 11:18:27 AM3/31/14
to Juan Torres, golang-nuts
On Mon, Mar 31, 2014 at 5:11 PM, Juan Torres <juantor...@gmail.com> wrote:

""""
Data must be a fixed-size value or a slice of fixed-size values, or a
pointer to such data.
""""[0]


"1" or "foo" or "whatever" is not a fixed size value.

[0]: http://golang.org/pkg/encoding/binary/#Write

-j

Gyepi SAM

unread,
Mar 31, 2014, 11:22:05 AM3/31/14
to Juan Torres, golan...@googlegroups.com
On Mon, Mar 31, 2014 at 08:11:37AM -0700, Juan Torres wrote:
> I´m try to convert a struct to a byte array, but i´m getting a error and
> i´m no sure it´s the best way,
> can help me?

encoding/binary doesn't encode structs.
encoding/gob and encoding/json do, among others.

However, I should ask, what are you trying to do?

-Gyepi

Juan Torres

unread,
Mar 31, 2014, 11:39:20 AM3/31/14
to golan...@googlegroups.com, Juan Torres, self-...@gyepi.com
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.. 

Juan Torres

unread,
Mar 31, 2014, 1:30:18 PM3/31/14
to golan...@googlegroups.com
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)

buf := new(bytes.Buffer)

    var number Number
    key := "1"
    number.One = key
    number.Two = "2"

    b, err := json.Marshal(number)
    if err != nil {
        fmt.Printf("Error: %s", err)
        return
    }
    fmt.Println(string(b))

    err = binary.Write(buf, binary.BigEndian, &b)

Gyepi SAM

unread,
Mar 31, 2014, 1:31:19 PM3/31/14
to Juan Torres, golan...@googlegroups.com
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. In that case encoding/gob
should work fine for you. Here's a usage example.

http://play.golang.org/p/frZq8YbcAb

Just FYI, there are other encoding mechanisms as well: json, bson, msgpack,
etc, etc. Depending on who else needs to read the data (humans included),
one or the other may be a better fit, but the process is about the same
in all cases.

-Gyepi

Gyepi SAM

unread,
Mar 31, 2014, 2:03:49 PM3/31/14
to Juan Torres, golan...@googlegroups.com

> err = binary.Write(buf, binary.BigEndian, &b)

This line is not necessary and is actually nonsensical.
b is of type []byte and already contains the value you need to store.
No need to involve binary at all.

-Gyepi
Reply all
Reply to author
Forward
0 new messages