FAQ
I'm trying to write a structure in binary format onto a specific LBA of the
disk. I want to be able to call something like this:
writeToDisk(deviceName, lba, &structInfo, size)

I would like to know if there is any straight forward way of doing this.

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

  • Dan Kortschak at Feb 16, 2013 at 1:06 am
    encoding/binary is your friend.

    --
    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 16, 2013 at 1:28 am
    Could you please elaborate a little further? How do I translate the
    structure to binary? Do I need a temp var for the translation and then
    write to the disk ?

    I see that for random access in binary files I need to use os.OpenFile()
    function ,then use the os.File.Seek() method to position ourselves in the
    file for reading or writing, or the os.File.ReadAt() But I wasn't able to
    find any examples online to do this. Is this even the right way of doing it?

    I have this in C:
    uint64_t writeLBA(int handle, uint64_t lba, void*buf, uint64_t bytes)
    {
    int ret = 0;
    uint64_t offset = lba * 512;

    while(bytes !=0 && (ret = pwrite(handle, buf, bytes, offset)) !=0)
    {
    if(ret == -1)
    {
    if(errno == EINTR)
    continue;
    perror("read:");
    return ret;
    }
    bytes -= ret;
    buf += ret;
    }

    return ret;
    }

    How do I do the same in golang?
    On Friday, February 15, 2013 6:06:46 PM UTC-7, kortschak wrote:

    encoding/binary is your friend.
    --
    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.
  • Dan Kortschak at Feb 16, 2013 at 1:48 am
    Yes, seek the correct location, then you can read with something like
    this:

    http://play.golang.org/p/mNouNnMOwW

    and write with something like this:

    http://play.golang.org/p/TSN52PtbzL

    Does this answer your question?

    --
    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.
  • Dan Kortschak at Feb 16, 2013 at 2:29 am
    By the way, both of those examples are pretty clearly presented in the package documentation for encoding/binary.

    http://golang.org/pkg/encoding/binary/#example_Read

    http://golang.org/pkg/encoding/binary/#example_Write

    The documentation for packages in the core library is generally outstanding (and nearly always at least good), often with examples you can play with on the browser. I'd strongly recommend it as a resource.

    --
    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 16, 2013 at 1:42 am
    Could you please elaborate a little further? How do I translate the
    structure to binary? Do I need a temp var for the translation and then
    write to the disk ?

    I see that for random access in binary files I need to use os.OpenFile()
    function ,then use the os.File.Seek() method to position ourselves in the
    file for reading or writing, or the os.File.ReadAt() But I wasn't able to
    find any examples online to do this. Is this even the right way of doing it?
    On Friday, February 15, 2013 6:06:46 PM UTC-7, kortschak wrote:

    encoding/binary is your friend.
    --
    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.
  • Konstantin Kulikov at Feb 16, 2013 at 8:53 am
    encoding/gob
    http://play.golang.org/p/TnFi5mr68m
    On Saturday, February 16, 2013 5:00:39 AM UTC+4, nko...@gmail.com wrote:

    I'm trying to write a structure in binary format onto a specific LBA of
    the disk. I want to be able to call something like this:
    writeToDisk(deviceName, lba, &structInfo, size)

    I would like to know if there is any straight forward way of doing this.

    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.
  • Dan Kortschak at Feb 16, 2013 at 9:22 am
    Although the OP didn't make this clear, they are probably trying to write to a block for the effect it has on the block, rather than to store some data. If this is they case, gob is not what they want. They also want to read from the block, and given they are asking, the block is most likely not gob encoded.
    On 16/02/2013, at 7:23 PM, "Konstantin Kulikov" wrote:

    encoding/gob
    http://play.golang.org/p/TnFi5mr68m
    --
    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.
  • Dan Kortschak at Feb 16, 2013 at 9:24 am
    This was a confabulation. Ignore this sentence.
    On 16/02/2013, at 7:52 PM, "Dan Kortschak" wrote:

    They also want to read from the block, and given they are asking, the block is most likely not gob encoded.
    --
    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 16, '13 at 1:00a
activeFeb 16, '13 at 9:24a
posts9
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase