FAQ
Need to read all the data In accordance with the structure.

package main

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

type AVP struct {
Type byte
Length byte
Values []byte
}

var (
avps AVP
)

func main() {
pack := []byte{6, 6, 0, 0, 0, 2, 7, 6, 0, 0, 0, 1, 8, 6, 185, 14, 32, 1, 9,
6, 255, 255, 255, 255, 27, 6, 0, 1, 81,
128, 26, 22, 0, 0, 0, 9, 250, 16, 65, 85, 78, 76, 95, 53, 48, 77, 95, 78,
73, 71, 72, 84, 26, 14, 0, 0, 0, 9,
250, 8, 65, 76, 79, 67, 65, 76, 26, 20, 0, 0, 0, 9, 250, 14, 65, 85, 78,
76, 95, 49, 48, 77, 95, 68, 65, 89}

buf := bytes.NewBuffer(pack)
binary.Read(buf, binary.BigEndian, &avps.Type)
binary.Read(buf, binary.BigEndian, &avps.Length)
avps.Values = make([]byte, avps.Length-2)
io.ReadFull(buf, avps.Values)
fmt.Println(avps)
}


The result should come out:
{6 6 [0 0 0 2]}
{7 6 [0 0 0 1[}
{8 6[185 14 32 1]}

etc.



--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Dave Cheney at Jul 28, 2014 at 7:06 am
    Did you forget to add a loop ? this code only decodes one avps structure,
    then exits.
    On Monday, 28 July 2014 14:35:58 UTC+10, Igor Avdoshkin wrote:

    Need to read all the data In accordance with the structure.

    package main

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

    type AVP struct {
    Type byte
    Length byte
    Values []byte
    }

    var (
    avps AVP
    )

    func main() {
    pack := []byte{6, 6, 0, 0, 0, 2, 7, 6, 0, 0, 0, 1, 8, 6, 185, 14, 32, 1,
    9, 6, 255, 255, 255, 255, 27, 6, 0, 1, 81,
    128, 26, 22, 0, 0, 0, 9, 250, 16, 65, 85, 78, 76, 95, 53, 48, 77, 95, 78,
    73, 71, 72, 84, 26, 14, 0, 0, 0, 9,
    250, 8, 65, 76, 79, 67, 65, 76, 26, 20, 0, 0, 0, 9, 250, 14, 65, 85, 78,
    76, 95, 49, 48, 77, 95, 68, 65, 89}

    buf := bytes.NewBuffer(pack)
    binary.Read(buf, binary.BigEndian, &avps.Type)
    binary.Read(buf, binary.BigEndian, &avps.Length)
    avps.Values = make([]byte, avps.Length-2)
    io.ReadFull(buf, avps.Values)
    fmt.Println(avps)
    }


    The result should come out:
    {6 6 [0 0 0 2]}
    {7 6 [0 0 0 1[}
    {8 6[185 14 32 1]}

    etc.


    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Carlos Castillo at Jul 28, 2014 at 9:23 pm
    Considering this code doesn't use external packages, post a playground link
    (play.golang.org). This way with runnable code we see the results exactly
    the same way you do, and can easily provide runnable code in return. This
    also ensures that nothing is "hidden" from us in a possible attempt to be
    succinct.

    For example, this code doesn't loop (as mentioned by Dave), nor does it
    check any errors. Both are obvious problems with the posted code, but may
    or may not have nothing to do with the problems you are having in the
    actual code.
    On Sunday, July 27, 2014 9:35:58 PM UTC-7, Igor Avdoshkin wrote:

    Need to read all the data In accordance with the structure.

    package main

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

    type AVP struct {
    Type byte
    Length byte
    Values []byte
    }

    var (
    avps AVP
    )

    func main() {
    pack := []byte{6, 6, 0, 0, 0, 2, 7, 6, 0, 0, 0, 1, 8, 6, 185, 14, 32, 1,
    9, 6, 255, 255, 255, 255, 27, 6, 0, 1, 81,
    128, 26, 22, 0, 0, 0, 9, 250, 16, 65, 85, 78, 76, 95, 53, 48, 77, 95, 78,
    73, 71, 72, 84, 26, 14, 0, 0, 0, 9,
    250, 8, 65, 76, 79, 67, 65, 76, 26, 20, 0, 0, 0, 9, 250, 14, 65, 85, 78,
    76, 95, 49, 48, 77, 95, 68, 65, 89}

    buf := bytes.NewBuffer(pack)
    binary.Read(buf, binary.BigEndian, &avps.Type)
    binary.Read(buf, binary.BigEndian, &avps.Length)
    avps.Values = make([]byte, avps.Length-2)
    io.ReadFull(buf, avps.Values)
    fmt.Println(avps)
    }


    The result should come out:
    {6 6 [0 0 0 2]}
    {7 6 [0 0 0 1[}
    {8 6[185 14 32 1]}

    etc.


    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • C Banning at Jul 29, 2014 at 9:24 am
    http://play.golang.org/p/bZ8lf7qQkO
    On Monday, July 28, 2014 4:23:05 PM UTC-5, Carlos Castillo wrote:

    Considering this code doesn't use external packages, post a playground
    link (play.golang.org). This way with runnable code we see the results
    exactly the same way you do, and can easily provide runnable code in
    return. This also ensures that nothing is "hidden" from us in a possible
    attempt to be succinct.

    For example, this code doesn't loop (as mentioned by Dave), nor does it
    check any errors. Both are obvious problems with the posted code, but may
    or may not have nothing to do with the problems you are having in the
    actual code.
    On Sunday, July 27, 2014 9:35:58 PM UTC-7, Igor Avdoshkin wrote:

    Need to read all the data In accordance with the structure.

    package main

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

    type AVP struct {
    Type byte
    Length byte
    Values []byte
    }

    var (
    avps AVP
    )

    func main() {
    pack := []byte{6, 6, 0, 0, 0, 2, 7, 6, 0, 0, 0, 1, 8, 6, 185, 14, 32, 1,
    9, 6, 255, 255, 255, 255, 27, 6, 0, 1, 81,
    128, 26, 22, 0, 0, 0, 9, 250, 16, 65, 85, 78, 76, 95, 53, 48, 77, 95, 78,
    73, 71, 72, 84, 26, 14, 0, 0, 0, 9,
    250, 8, 65, 76, 79, 67, 65, 76, 26, 20, 0, 0, 0, 9, 250, 14, 65, 85, 78,
    76, 95, 49, 48, 77, 95, 68, 65, 89}

    buf := bytes.NewBuffer(pack)
    binary.Read(buf, binary.BigEndian, &avps.Type)
    binary.Read(buf, binary.BigEndian, &avps.Length)
    avps.Values = make([]byte, avps.Length-2)
    io.ReadFull(buf, avps.Values)
    fmt.Println(avps)
    }


    The result should come out:
    {6 6 [0 0 0 2]}
    {7 6 [0 0 0 1[}
    {8 6[185 14 32 1]}

    etc.


    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJul 28, '14 at 4:36a
activeJul 29, '14 at 9:24a
posts4
users4
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase