Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
168 views
in Technique[技术] by (71.8m points)

go - GoLang - Populate Array of Structure

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)
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...