I am trying to learn Golang and have experience in Python. I'm currently trying to figure out how to pack variables into binary format (with checksum). In Python, I would use something like:
import struct import hashlib a = 100 b = "foo\x00\x00"
To do the same in Go, I try code like this:
package main import ( "crypto/sha256" "fmt" "encoding/binary" "bytes" ) type packet struct { a uint8 b string } func main() { var p = packet{} pa = 1 pb = "foo\x00\x00" buf := new(bytes.Buffer) binary.Write(buf, binary.LittleEndian, &p) h := sha256.New() h.Write(buf.String()) fmt.Printf("% x\n", p) }
Unfortunately, however, I am attacking this; I seem to fall into a nightmare when confronted with variable types (buffers, byte arrays and strings). I would appreciate some advice on whether I even take a remotely correct approach.
python go
Steve crook
source share