doudieyou5209 2017-10-25 20:51
浏览 352

GoLang-填充结构数组

I have the following 2 structures

     type AAAA struct{
        Aa  [2]byte
        Ab  [2]byte
        Ac  [3]byte
    }       
    type BBBB struct{
        Ba  [4]byte
        Bb  [2]byte
        Bc  [3]byte
        Bd  [2]byte   // No Of Struct AAA Items
        BBStr  []AAAA
    }

So Struct BBB is repeating in Struct AAA

Then I have a string that I have as input which contains the value of the structure coming as input

input := "aaaabbccc02ddeefffddeefff" (here 02 is no of time Struct AAAA repeats in Struct BBBB)

I need to read through the input string and populate the structure BBBB including the array of structure AAA

I wrote the function below to achieve this. But I am getting " Bad error - binary.Read: invalid type *main.BBBB

Need Help in identifying why this error? Also is there a way to do this differently?

    func main() {
        input := "aaaabbccc02ddeefffddeefff"
        var k BBBB
        var j AAAA
        k.BBStr = append(k.BBStr,j)
        k.BBStr = append(k.BBStr,j)
        xyz := []byte(input)
        err := binary.Read(bytes.NewReader(xyz), binary.LittleEndian, &k)
        if err != nil {
            fmt.Println("Bad error - ",err)
        }
        fmt.Println("Structure Definition - ",k)
    }
  • 写回答

1条回答 默认 最新

  • dowe98261 2017-10-25 21:11
    关注

    The documentation at https://golang.org/pkg/encoding/binary/#Read says:

    Data must be a pointer to a fixed-size value or a slice of fixed-size values.

    which means that the size of the type you Read must be known at compile time. Your struct BBBB contains a slice of []AAAA which means its length is known only at run-time. If you make it a fixed-size array, e.g. [16]AAAA, it would work.

    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器