FAQ
I'm trying to to read a binary file into some typed structure of data, and
I came up with this solution (via the encoding/binary package) that works
fine, but seems kinda complicated when compared to what I'd have to do in
C. I'm new to Go and I want to know if there is a better way of doing the
same. Any suggestions/ideas on improving this?

type array_entity_t struct {
Id uint8
Sn [256]byte
}

type array_save_t struct {
Count uint8
Array [64]array_entity_t
Checksum uint8
}

func main() {
var err error

save := new(array_save_t)

totalSize := (unsafe.Sizeof(*save))
chars := make([]byte, totalSize)

fi, err := syscall.Open(HCA, syscall.O_RDONLY, 0)
if err != nil {
panic(err)
}
defer syscall.Close(fi)

_, errno := syscall.Read(fi, chars)
if err = os.NewSyscallError("SYS_READ", errno); err != nil {
panic(err)
}

reader := strings.NewReader(string(chars[0:]))
if err = binary.Read(reader, binary.LittleEndian, save); err != nil {
panic(err)
}

// blah

}

Thank you!

--
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/groups/opt_out.

Search Discussions

  • Dave Cheney at Feb 5, 2013 at 11:48 pm
    You want os.Open, which returns an io.ReadWriteCloser. Also, don't discard
    the number of bytes read from Read, you need that.
    On Wednesday, February 6, 2013, wrote:

    I'm trying to to read a binary file into some typed structure of data, and
    I came up with this solution (via the encoding/binary package) that works
    fine, but seems kinda complicated when compared to what I'd have to do in
    C. I'm new to Go and I want to know if there is a better way of doing the
    same. Any suggestions/ideas on improving this?

    type array_entity_t struct {
    Id uint8
    Sn [256]byte
    }

    type array_save_t struct {
    Count uint8
    Array [64]array_entity_t
    Checksum uint8
    }

    func main() {
    var err error

    save := new(array_save_t)

    totalSize := (unsafe.Sizeof(*save))
    chars := make([]byte, totalSize)

    fi, err := syscall.Open(HCA, syscall.O_RDONLY, 0)
    if err != nil {
    panic(err)
    }
    defer syscall.Close(fi)

    _, errno := syscall.Read(fi, chars)
    if err = os.NewSyscallError("SYS_READ", errno); err != nil {
    panic(err)
    }

    reader := strings.NewReader(string(chars[0:]))
    if err = binary.Read(reader, binary.LittleEndian, save); err != nil {
    panic(err)
    }

    // blah

    }

    Thank you!

    --
    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 <javascript:_e({},
    'cvml', 'golang-nuts%2Bunsubscribe@googlegroups.com');>.
    For more options, visit https://groups.google.com/groups/opt_out.

    --
    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/groups/opt_out.
  • Nkothap at Feb 5, 2013 at 11:58 pm
    os.Open returns a *os.File, and syscall.Read() takes int as a param. So I
    had to change that to syscall.Open.

    Can you please be a little more explicit?
    On Tuesday, February 5, 2013 4:48:11 PM UTC-7, Dave Cheney wrote:

    You want os.Open, which returns an io.ReadWriteCloser. Also, don't
    discard the number of bytes read from Read, you need that.
    On Wednesday, February 6, 2013, wrote:

    I'm trying to to read a binary file into some typed structure of data,
    and I came up with this solution (via the encoding/binary package) that
    works fine, but seems kinda complicated when compared to what I'd have to
    do in C. I'm new to Go and I want to know if there is a better way of doing
    the same. Any suggestions/ideas on improving this?

    type array_entity_t struct {
    Id uint8
    Sn [256]byte
    }

    type array_save_t struct {
    Count uint8
    Array [64]array_entity_t
    Checksum uint8
    }

    func main() {
    var err error

    save := new(array_save_t)

    totalSize := (unsafe.Sizeof(*save))
    chars := make([]byte, totalSize)

    fi, err := syscall.Open(HCA, syscall.O_RDONLY, 0)
    if err != nil {
    panic(err)
    }
    defer syscall.Close(fi)

    _, errno := syscall.Read(fi, chars)
    if err = os.NewSyscallError("SYS_READ", errno); err != nil {
    panic(err)
    }

    reader := strings.NewReader(string(chars[0:]))
    if err = binary.Read(reader, binary.LittleEndian, save); err != nil {
    panic(err)
    }

    // blah

    }

    Thank you!

    --
    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/groups/opt_out.

    --
    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/groups/opt_out.
  • Dave Cheney at Feb 6, 2013 at 12:04 am
    Sure,

    http://play.golang.org/p/1KQv5WCqc9

    TL;DR - using syscall.Anything is a code smell.
    On Wed, Feb 6, 2013 at 10:58 AM, wrote:
    os.Open returns a *os.File, and syscall.Read() takes int as a param. So I
    had to change that to syscall.Open.

    Can you please be a little more explicit?
    On Tuesday, February 5, 2013 4:48:11 PM UTC-7, Dave Cheney wrote:

    You want os.Open, which returns an io.ReadWriteCloser. Also, don't discard
    the number of bytes read from Read, you need that.
    On Wednesday, February 6, 2013, wrote:

    I'm trying to to read a binary file into some typed structure of data,
    and I came up with this solution (via the encoding/binary package) that
    works fine, but seems kinda complicated when compared to what I'd have to do
    in C. I'm new to Go and I want to know if there is a better way of doing the
    same. Any suggestions/ideas on improving this?

    type array_entity_t struct {
    Id uint8
    Sn [256]byte
    }

    type array_save_t struct {
    Count uint8
    Array [64]array_entity_t
    Checksum uint8
    }

    func main() {
    var err error

    save := new(array_save_t)

    totalSize := (unsafe.Sizeof(*save))
    chars := make([]byte, totalSize)

    fi, err := syscall.Open(HCA, syscall.O_RDONLY, 0)
    if err != nil {
    panic(err)
    }
    defer syscall.Close(fi)

    _, errno := syscall.Read(fi, chars)
    if err = os.NewSyscallError("SYS_READ", errno); err != nil {
    panic(err)
    }

    reader := strings.NewReader(string(chars[0:]))
    if err = binary.Read(reader, binary.LittleEndian, save); err != nil {
    panic(err)
    }

    // blah

    }

    Thank you!

    --
    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/groups/opt_out.
    --
    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/groups/opt_out.
    --
    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/groups/opt_out.
  • Dave Cheney at Feb 6, 2013 at 12:09 am
    Also, https://gobyexample.com/reading-files

    On Wed, Feb 6, 2013 at 11:04 AM, Dave Cheney wrote:
    Sure,

    http://play.golang.org/p/1KQv5WCqc9

    TL;DR - using syscall.Anything is a code smell.
    On Wed, Feb 6, 2013 at 10:58 AM, wrote:
    os.Open returns a *os.File, and syscall.Read() takes int as a param. So I
    had to change that to syscall.Open.

    Can you please be a little more explicit?
    On Tuesday, February 5, 2013 4:48:11 PM UTC-7, Dave Cheney wrote:

    You want os.Open, which returns an io.ReadWriteCloser. Also, don't discard
    the number of bytes read from Read, you need that.
    On Wednesday, February 6, 2013, wrote:

    I'm trying to to read a binary file into some typed structure of data,
    and I came up with this solution (via the encoding/binary package) that
    works fine, but seems kinda complicated when compared to what I'd have to do
    in C. I'm new to Go and I want to know if there is a better way of doing the
    same. Any suggestions/ideas on improving this?

    type array_entity_t struct {
    Id uint8
    Sn [256]byte
    }

    type array_save_t struct {
    Count uint8
    Array [64]array_entity_t
    Checksum uint8
    }

    func main() {
    var err error

    save := new(array_save_t)

    totalSize := (unsafe.Sizeof(*save))
    chars := make([]byte, totalSize)

    fi, err := syscall.Open(HCA, syscall.O_RDONLY, 0)
    if err != nil {
    panic(err)
    }
    defer syscall.Close(fi)

    _, errno := syscall.Read(fi, chars)
    if err = os.NewSyscallError("SYS_READ", errno); err != nil {
    panic(err)
    }

    reader := strings.NewReader(string(chars[0:]))
    if err = binary.Read(reader, binary.LittleEndian, save); err != nil {
    panic(err)
    }

    // blah

    }

    Thank you!

    --
    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/groups/opt_out.
    --
    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/groups/opt_out.
    --
    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/groups/opt_out.
  • Nkothap at Feb 6, 2013 at 12:12 am
    Works. Thank you! Looks like I complicated it a lot more now!
    On Tuesday, February 5, 2013 5:04:28 PM UTC-7, Dave Cheney wrote:

    Sure,

    http://play.golang.org/p/1KQv5WCqc9

    TL;DR - using syscall.Anything is a code smell.
    On Wed, Feb 6, 2013 at 10:58 AM, <nko...@gmail.com <javascript:>> wrote:
    os.Open returns a *os.File, and syscall.Read() takes int as a param. So I
    had to change that to syscall.Open.

    Can you please be a little more explicit?
    On Tuesday, February 5, 2013 4:48:11 PM UTC-7, Dave Cheney wrote:

    You want os.Open, which returns an io.ReadWriteCloser. Also, don't
    discard
    the number of bytes read from Read, you need that.
    On Wednesday, February 6, 2013, wrote:

    I'm trying to to read a binary file into some typed structure of data,
    and I came up with this solution (via the encoding/binary package)
    that
    works fine, but seems kinda complicated when compared to what I'd have
    to do
    in C. I'm new to Go and I want to know if there is a better way of
    doing the
    same. Any suggestions/ideas on improving this?

    type array_entity_t struct {
    Id uint8
    Sn [256]byte
    }

    type array_save_t struct {
    Count uint8
    Array [64]array_entity_t
    Checksum uint8
    }

    func main() {
    var err error

    save := new(array_save_t)

    totalSize := (unsafe.Sizeof(*save))
    chars := make([]byte, totalSize)

    fi, err := syscall.Open(HCA, syscall.O_RDONLY, 0)
    if err != nil {
    panic(err)
    }
    defer syscall.Close(fi)

    _, errno := syscall.Read(fi, chars)
    if err = os.NewSyscallError("SYS_READ", errno); err != nil {
    panic(err)
    }

    reader := strings.NewReader(string(chars[0:]))
    if err = binary.Read(reader, binary.LittleEndian, save); err !=
    nil {
    panic(err)
    }

    // blah

    }

    Thank you!

    --
    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...@googlegroups.com <javascript:>.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    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...@googlegroups.com <javascript:>.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    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/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedFeb 5, '13 at 11:46p
activeFeb 6, '13 at 12:12a
posts6
users2
websitegolang.org

2 users in discussion

Dave Cheney: 3 posts Nkothap: 3 posts

People

Translate

site design / logo © 2022 Grokbase