Golang中的低级磁盘I/O

huangapple go评论89阅读模式
英文:

Low-level disk I/O in Golang

问题

我想知道是否有人在进行低级磁盘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.

答案1

得分: 9

我对Go语言还不太熟悉,所以我的示例可能不是特别优雅,但我认为这是你想要的:

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("读取的字节数: %d\n", numread)
	fmt.Printf("缓冲区内容: %b\n", buffer)

	err = syscall.Close(fd)

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

这是syscall包的文档链接:http://golang.org/pkg/syscall/

根据这个页面,该包试图与尽可能多的不同平台兼容,但在我这个初学者的眼中,主要目标似乎是Linux API,当然,还有一些Go的惯用法来简化事情。希望这回答了你的问题!

英文:

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")
	}
}

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

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!

答案2

得分: 0

似乎已经很晚了,但对其他人可能还是有趣的。这里有一个有趣的例子,展示了如何在Windows上使用MBR:GoogleCloudPlatform/compute-image-tools,他们使用了golang.org/x/sys/windows这个包。

英文:

It seems it is already late, but it might be interesting for somebody else. There is an interesting example of how to work with MBR on Windows:
GoogleCloudPlatform/compute-image-tools

and they use package golang.org/x/sys/windows

huangapple
  • 本文由 发表于 2014年1月10日 06:19:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/21032426.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定