Golang中的底层磁盘I / O [英] Low-level disk I/O in Golang

查看:146
本文介绍了Golang中的底层磁盘I / O的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想知道是否有人在尝试低级别的磁盘I / O,例如读取原始扇区,MBR等。我已经做了一些自我探索,但未能找到任何提及的内容。在有人谈论Go的本机 io 软件包时,大多数情况都是死胡同。

Wondering if there has been anyone experimenting with low-level disk I/O, such as reading raw sectors, MBR, etc. I've done some digging around myself, but haven't been able to find anything mentioned about it. Most of it is dead ends where someone is talking about Go's native io package.

任何线索都将不胜感激。

Any leads would be appreciated.

推荐答案

我仍然很陌生,因此我的示例并不是特别优雅,但是我认为这就是您想要的:

I am still new to go so my example is not particularly elegant, but I think this is what you want:

package main

import (
    "syscall"
    "fmt"
)

func main() {
    disk := "/dev/sda"
    var fd, numread int
    var err error

    fd, err = syscall.Open(disk, syscall.O_RDONLY, 0777)

    if err != nil {
        fmt.Print(err.Error(), "\n")
        return
    }

    buffer := make([]byte, 10, 100)

    numread, err = syscall.Read(fd, buffer)

    if err != nil {
        fmt.Print(err.Error(), "\n")
    }

    fmt.Printf("Numbytes read: %d\n", numread)
    fmt.Printf("Buffer: %b\n", buffer)

    err = syscall.Close(fd)

    if err != nil {
        fmt.Print(err.Error(), "\n")
    }
}

此处是syscall软件包文档的链接: http://golang.org/pkg/syscall/

Here is a link to the syscall package documentation: http://golang.org/pkg/syscall/

根据此页面,此软件包尝试与许多不同的平台兼容尽可能地,但是在我的新手看来,主要目标是Linux API,当然还有惯用语来简化事情。我希望这能回答您的问题!

According to this page, this package attempts to be compatible with as many different platforms as possible but it kinda seems to my novice eye like the main target is the Linux API with, of course, go idioms to simplify things. I hope this answers your question!

这篇关于Golang中的底层磁盘I / O的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆