函数修改字节切片参数。

huangapple go评论82阅读模式
英文:

Function mutates byte slice argument

问题

我有以下代码,其中我有一个字节切片包含字母表,我将这个字母表复制到一个新变量(cryptkey)中,并使用一个函数对其进行洗牌。结果是字母表和cryptkey字节切片被洗牌了。我该如何防止这种情况发生?

package main

import (
	"fmt"
	"math/rand"
)

func main() {
	alphabet := []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz.")
	cryptkey := make([]byte, len(alphabet))
	copy(cryptkey, alphabet)
	fmt.Println(string(alphabet))
	cryptkey = shuffle(cryptkey)
	fmt.Println(string(alphabet))
}

func shuffle(b []byte) []byte {
	l := len(b)
	out := make([]byte, l)
	copy(out, b)
	for key := range out {
		dest := rand.Intn(l)
		out[key], out[dest] = out[dest], out[key]
	}
	return out
}

结果:

ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz.
miclOfEInzJNvZe.YuVMCdTbXyqtaLwHGjUrABhog xQPWSpKRkDsF

Playground

英文:

I have the following code where I have a slice of bytes with the alphabet, I copy this alphabet array in a new variable (cryptkey) and I use a function to shuffle it. The result is that the alphabet and the cryptkey byte slice get shuffled. How can I prevent this from happening?

package main

import (
	"fmt"
	"math/rand"
)

func main() {
	alphabet := []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz.")
	cryptkey := alphabet
	fmt.Println(string(alphabet))
	cryptkey = shuffle(cryptkey)
	fmt.Println(string(alphabet))
}

func shuffle(b []byte) []byte {
	l := len(b)
	out := b
	for key := range out {
		dest := rand.Intn(l)
		out[key], out[dest] = out[dest], out[key]
	}
	return out
}

Result :
>ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz.
miclOfEInzJNvZe.YuVMCdTbXyqtaLwHGjUrABhog xQPWSpKRkDsF

Playground!

答案1

得分: 3

复制一份。例如,

package main

import (
	"fmt"
	"math/rand"
)

func main() {
	alphabet := []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz.")
	cryptkey := alphabet
	fmt.Println(string(alphabet))
	cryptkey = shuffle(cryptkey)
	fmt.Println(string(alphabet))
}

func shuffle(b []byte) []byte {
	l := len(b)
	out := append([]byte(nil), b...)
	for key := range out {
		dest := rand.Intn(l)
		out[key], out[dest] = out[dest], out[key]
	}
	return out
}

输出:

ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz.
ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz.
英文:

Make a copy. For example,

package main

import (
	"fmt"
	"math/rand"
)

func main() {
	alphabet := []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz.")
	cryptkey := alphabet
	fmt.Println(string(alphabet))
	cryptkey = shuffle(cryptkey)
	fmt.Println(string(alphabet))
}

func shuffle(b []byte) []byte {
	l := len(b)
	out := append([]byte(nil), b...)
	for key := range out {
		dest := rand.Intn(l)
		out[key], out[dest] = out[dest], out[key]
	}
	return out
}

Output:

ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz.
ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz.

huangapple
  • 本文由 发表于 2014年12月13日 23:32:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/27460494.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定