arrays - 无法写入文件
问题描述
我尝试使用 go 编写 OneTimePad,但无法写入文件:文件是 bin 文件(编译后的 Go 代码) 我的代码:
package main
import ("fmt"
"io/ioutil"
"math/rand")
func rndByte(l int)[]byte{
token := make([]byte, l)
rand.Read(token)
return token
}
func writeByteFile(filename string,inp []byte ){
err := ioutil.WriteFile(filename, inp, 0644)
if err != nil {
fmt.Println(err)
}
}
func readFile(filename string) []byte {
data, err := ioutil.ReadFile(filename)
if err != nil {
fmt.Println("File reading error", err)
}
return data
}
func main(){
x := readFile("xor")
// y:= len(x)
z := rndByte(489)
var res [489]byte
for i:=0; i != 489; i++{
res[i] = x[i] ^ z[i]
}
writeByteFile("xorKey", z)
writeByteFile("xorENC", res)
}
我的错误:
# command-line-arguments ./xorbyte.go:47:19: 在 writeByteFile 的参数中不能使用 res (type [489]byte) 作为 type []byte
解决方案
[489]byte
并且[]byte
是不同的类型。
[489]byte
是一个数组
[]byte
是一片
尝试将数组转换为切片:
writeByteFile("xorENC", res[:])