gpt4 book ai didi

arrays - 无法写入文件

转载 作者:行者123 更新时间:2023-12-01 22:38:43 25 4
gpt4 key购买 nike

我尝试使用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)
}



我的错误:

#命令行参数
./xorbyte.go:47:19:在writeByteFile的参数中不能使用res([489] byte类型)作为[] byte类型

最佳答案

[489]byte[]byte是不同的类型。
[489]byte是一个数组
[]byte是一个 slice

尝试将数组转换为 slice :

writeByteFile("xorENC", res[:])

结帐 https://blog.golang.org/go-slices-usage-and-internals

关于arrays - 无法写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60635055/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com