Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将字节数组转换为Golang中的syscall.InotifyEvent结构_Go_Inotify - Fatal编程技术网

将字节数组转换为Golang中的syscall.InotifyEvent结构

将字节数组转换为Golang中的syscall.InotifyEvent结构,go,inotify,Go,Inotify,我正在使用golang的系统调用库中的inotify功能。我可以使用InotifyInit设置inotify功能,使用InotifyAddWatch添加要查看的文件,并使用Read功能检测文件更改。我遇到的问题是,Read函数只读取包含inotify事件信息的字节数组。我希望将该字节数组转换/强制转换为InotifyEvent结构,以便正确访问有关inotify事件的信息 以下是我目前的情况: package main import ( "fmt" "syscall" ) f

我正在使用golang的系统调用库中的inotify功能。我可以使用
InotifyInit
设置inotify功能,使用
InotifyAddWatch
添加要查看的文件,并使用
Read
功能检测文件更改。我遇到的问题是,
Read
函数只读取包含inotify事件信息的字节数组。我希望将该字节数组转换/强制转换为
InotifyEvent
结构,以便正确访问有关inotify事件的信息

以下是我目前的情况:

package main

import (
    "fmt"
    "syscall"
)

func main() {
    buff := make([]byte, 64)
    inotefd, err := syscall.InotifyInit()
    if err != nil {
        fmt.Println(err)
    }
    _, err = syscall.InotifyAddWatch(inotefd, "/home/me/foo", syscall.IN_MODIFY)
    if err != nil {
        fmt.Println(err)
    }

    for {
        n, err := syscall.Read(inotefd, buff)
        if err != nil {
            fmt.Println(err)
            return
        }

        if n < 0 {
            fmt.Println("Read Error")
            return
        }

        fmt.Printf("Buffer: %v\n", buff)
        //can't cast []buff into InotifyEvent struct
        fmt.Printf("Cookie: %v\n", buff[0:4])
        fmt.Printf("Len: %v\n", buff[4:8])
        fmt.Printf("Mask: %v\n", buff[8:12])
        fmt.Printf("Name: %v\n", buff[12:13])
        fmt.Printf("Wd: %v\n", buff[13:17])
    }
}
主程序包
进口(
“fmt”
“系统调用”
)
func main(){
buff:=make([]字节,64)
inotefd,err:=syscall.InotifyInit()
如果错误!=零{
fmt.Println(错误)
}
_,err=syscall.InotifyAddWatch(inotefd,“/home/me/foo”,syscall.IN_MODIFY)
如果错误!=零{
fmt.Println(错误)
}
为了{
n、 错误:=syscall.Read(inotefd,buff)
如果错误!=零{
fmt.Println(错误)
返回
}
如果n<0{
fmt.Println(“读取错误”)
返回
}
fmt.Printf(“缓冲区:%v\n”,buff)
//无法将[]buff强制转换到InotifyEvent结构中
fmt.Printf(“Cookie:%v\n”,buff[0:4])
fmt.Printf(“镜头:%v\n”,浅黄色[4:8])
fmt.Printf(“掩码:%v\n”,buff[8:12])
fmt.Printf(“名称:%v\n”,buff[12:13])
fmt.Printf(“Wd:%v\n”,buff[13:17])
}
}

谢谢你的帮助

您可以使用
不安全的
软件包:

    info := *((*syscall.InotifyEvent)(unsafe.Pointer(&buff[0])))