lunixbochs / struc

Better binary packing for Go
MIT License
564 stars 42 forks source link

Basic example failing on goplayground #83

Closed mikesol closed 4 years ago

mikesol commented 4 years ago

I'm not sure if this is a bug in goplayground, my code, or struc, but I figured I'd report it here in case there's something obvious I'm missing.

https://play.golang.org/p/xW7R-0YjQ8n

package main

import (
    "fmt"
    "bytes"
    "github.com/lunixbochs/struc"

)

type I struct {
    i float32 `struc:"float32"`
}

type O struct {
    o int `struc:"int32"`
}

func float32ToInt32(f float32) int {
    var buf bytes.Buffer
    t := &I{f}
    e := struc.Pack(&buf, t)
    if (e != nil) {
        fmt.Println("encoding error", e)
    }
    o := &O{}
    ee := struc.Unpack(&buf, o)
    if (e != nil) {
        fmt.Println("decoding error", ee)
    }
    return o.o
}

func main() {
  fmt.Println("float32ToInt32", float32ToInt32(32.3))
}

Results in the incorrect output:

float32ToInt32 0
lunixbochs commented 4 years ago

Can't pack private fields.

lunixbochs commented 4 years ago

You don't need the structs either: https://play.golang.org/p/CMl1QiG4f0C