We need to wipe out some variables after use. But it seems really weird when it's assigned with a []byte
field in a struct.
Why this assignment of []byte
is not a copy but a pointer?
What should I do to keep the value in struct a.bs
, but wipe out the b
as local variable?
http://play.golang.org/p/MT_wAHj2OM
package main
import "fmt"
type so struct {
bs []byte
}
func zeroes(n int) []byte {
return make([]byte, n)
}
func wipeBytes(b []byte) {
copy(b, zeroes(len(b)))
}
func main() {
a := so{bs: []byte{0x01, 0x02}}
b := a.bs
wipeBytes(b)
fmt.Println(b) //b == []byte{}
fmt.Println(a.bs) //a.bs == []byte{}
}