11
4

More than 5 years have passed since last update.

Go言語でシンプルに構造体⇔バイナリの相互変換

Posted at

構造体をサクッとバイナリに変換したい!

そんなときはencoding/binaryパッケージを使います。

構造体→バイナリ

binary.Writeメソッドに構造体を渡すと上から順番にフィールドが並んだバイナリになります。

package main

import (
    "encoding/binary"
    "encoding/hex"
    "os"
)

type A struct {
    Int32Field int32
    ByteField byte
}

func main() {
    // 構造体つくる
    a := A{Int32Field: 0x123456, ByteField: 0xFF}

    stdoutDumper := hex.Dumper(os.Stdout)
    defer stdoutDumper.Close()

    // 構造体をバイナリにする
    binary.Write(stdoutDumper, binary.LittleEndian, &a)
}

出力

00000000  56 34 12 00 ff                                    |V4...|

バイナリ→構造体

binary.Readメソッドにバイナリと受けの構造体を渡すと、構造体に復元できます。

package main

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

type A struct {
    Int32Field int32
    ByteField byte
}

func main() {
    // 構造体つくる
    a := A{}

    // バイナリを構造体aに入れる
    bin := []byte("\x56\x34\x12\x00\xFF")
    reader := bytes.NewReader(bin)
    binary.Read(reader, binary.LittleEndian, &a)

    fmt.Printf("a: %v\n", a)
}

出力

a: {1193046 255}

注意点

ポインタやスライスなどを含む場合

binary.Read/Write ではポインタやスライスなどの複雑な型のフィールドは扱えません。

そのような複雑なものについては、gobprotobufを検討すると良いかもしれません

11
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
4