英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论