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
在GO编程中,如何在特定索引处更改字节[]中的某些字节?_Go_Byte - Fatal编程技术网

在GO编程中,如何在特定索引处更改字节[]中的某些字节?

在GO编程中,如何在特定索引处更改字节[]中的某些字节?,go,byte,Go,Byte,我有一个[]字节,它本质上是一个字符串,在这个数组中,我发现了一些我想使用索引更改的内容: content []byte key []byte newKey []byte i = bytes.Index(content, key) 所以我在内容中找到了key(在索引I中),现在我想用newKey替换key,但我似乎找不到一种方法将其添加到内容中,我尝试了一种显然不起作用的方法:) 是否有一些函数允许我在content[]字节内用“newKey”替换“key” 谢谢,在文章“”之后,您可以使用c

我有一个[]字节,它本质上是一个字符串,在这个数组中,我发现了一些我想使用索引更改的内容:

content []byte
key []byte
newKey []byte
i = bytes.Index(content, key)
所以我在内容中找到了key(在索引I中),现在我想用newKey替换key,但我似乎找不到一种方法将其添加到内容中,我尝试了一种显然不起作用的方法:)

是否有一些函数允许我在content[]字节内用“newKey”替换“key”

谢谢,

在文章“”之后,您可以使用
copy
创建包含正确内容的片段:

输出:

[0 0 97 98 0 0 0 0 0 0]
[0 0 97 98 99 100 0 0 0 0] [98 99]
[0 0 97 120 121 100 0 0 0 0] [120 121]

在您的情况下,如果
len(key)==len(newJey)

输出:

[0 0 97 98 0 0 0 0 0 0]
[0 0 97 98 99 100 0 0 0 0] [98 99]
[0 0 97 120 121 100 0 0 0 0] [120 121]
在文章“”之后,您可以使用
copy
创建具有正确内容的片段:

输出:

[0 0 97 98 0 0 0 0 0 0]
[0 0 97 98 99 100 0 0 0 0] [98 99]
[0 0 97 120 121 100 0 0 0 0] [120 121]

在您的情况下,如果
len(key)==len(newJey)

输出:

[0 0 97 98 0 0 0 0 0 0]
[0 0 97 98 99 100 0 0 0 0] [98 99]
[0 0 97 120 121 100 0 0 0 0] [120 121]

使用复制检查:但是需要len(newKey)==len(key)如果[]字节本质上是字符串,为什么不使用strings包
content=[]字节(strings.Replace(string(content)、string(key)、string(newKey)))
使用复制检查:但是需要len(newKey)==len(key)如果[]字节本质上是字符串,为什么不使用strings包<代码>内容=[]字节(字符串。替换(字符串(内容)、字符串(键)、字符串(新键))