python - 写入后无法使用 go 从文件中读取字节

标签 python go io byte

所以,我正在尝试在 golang 中制作一个简单的 AOT 虚拟机,它在输入时读取字节码文件。我基本上是在尝试将字节写入文件,然后使用 ioutil 读取它们,但是我遇到了 null 取消引用错误。

这是我用于写入文件的 python 代码:

btest = open("test.thief", "w")
bytes_to_write = bytearray([1, 44, 56, 55, 55, 0])

btest.write(bytes_to_write)

btest.close()

这是我用来读取字节的 go 文件中的代码

package main

import (
   "fmt"
   "io/ioutil"
   "os"
)

func main() {
  //gets command line args
    userArgs := os.Args[1:]

    bytes, err := ioutil.ReadFile(userArgs[0]) // just pass the file name
    if err != nil {
        fmt.Print(err)
    }

     fmt.Println(bytes)
     machine := NewEnv()
     ReadBytes(machine, bytes)

}

这是我得到的错误:

Joshuas-MacBook-Pro-3:thief Josh$ ./bin/Thief test.thief
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x0 pc=0x2af3]

goroutine 1 [running]:
main.ReadBytes(0xc82000e390, 0xc82000c480, 0x6, 0x206)
    /Users/Josh/thief/src/Thief/read.go:26 +0x353
main.main()
    /Users/Josh/thief/src/Thief/Main.go:18 +0x1f2

我也试过用 python 代码以二进制模式打开文件,但它产生了同样的错误。基本上这样做的目标是我希望能够提取字节数组,但也有一种编写主题的方法。

我写错字节了吗?

这是十六进制转储:

012c 3837 3700

编辑:

这是我剩下的go文件

opfuncs.go

package main


//file for machine functions and operations

//prints string format of some bytes
func print(env Env, data []byte) {
    fmt.Println(string(data))
}


var OPERS map[byte]func(Env, []byte)

//0 is reserved as the null terminator
func init(){
    OPERS = map[byte]func(Env, []byte){
        1:print,
    }
}

env.go

package main


//file for the env struct



type Env struct {
    items map[string]interface{}
}

func NewEnv() Env {
    return Env{make(map[string]interface{})}
}

//general getter
func (self *Env) get(key string) interface{} {
    val, has := self.items[key]
    if has {
        return val
    } else {
        return nil
    }
}

//general setter
func (self *Env) set(key string, value interface{}) {
    self.items[key] = value
}

func (self *Env) contains(key string) bool {
    _, has := self.items[key]
    return has
}

func (self *Env) declare(key string) {
    self.items[key] = Null{}
}

func (self *Env) is_null(key string) bool {
    _, ok := self.items[key].(Null)
    return ok
}

//deletes an item from storage
func (self *Env) del(key string) {
    delete(self.items, key)
}

阅读.go

package main

//file that contains the reading function for the VM

func ReadBytes(env Env, in []byte) {
    bytes := make([]byte, 1)
    fn := byte(0)
    mode := 0
    for i:=0;i<len(in);i++ {
        switch mode {
        case 0:
            fn = byte(i)
            mode = 1
        case 1:
            if i != len(in)-1 {
                if in[i+1] == 0 {
                    bytes = append(bytes, in[i])
                    mode = 2
                } else {
                    bytes = append(bytes, in[i])
                }
            } else {
                bytes = append(bytes, in[i])
            }
        case 2:
            OPERS[fn](env, bytes)
            mode = 0
            fn = byte(0)
            bytes = make([]byte, 1)
        }
    }
}

最佳答案

如果使用 Python 3,文件必须以二进制模式打开:

btest = open("test.thief", "wb")

关于python - 写入后无法使用 go 从文件中读取字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41647802/

相关文章:

json - 将具有重复字段的字符串解码为 json

Python IO 不可哈希列表正则表达式

c++ - Isstringstream 不适用于整数

go - Protoc 不创建服务

linux - 我可以将 O_DIRECT 用于写入请求以避免在电源故障期间丢失数据吗?

Python ctypes : Passing an array of strings

python - 有没有办法在 django 中测试具有大量 raw_input 的方法?

python - 如何将azure事件网格中的事件发布到python中的主题?

python - 预期交互式解析失败?

go - golang中的并发设计——等待一定数量的goroutines