Go: deep copy slices

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

Go: deep copy slices

问题

我想读取一个表示十六进制数的字符串切片,并将它们解码为字节切片的切片([]string --> [][]byte)。这是我目前的代码:

func (self *algo_t) decode_args(args []string) ([][]byte, error) {
    var data [][]byte
    for i := uint32(0); i < self.num_args; i++ {
        data = make([][]byte, self.num_args)
        tmp, err := hex.DecodeString(args[i])
        fmt.Printf("i = %d\ttmp = %x\n", i, tmp)
        data[i] = make([]byte, len(tmp))
        copy(data[i], tmp)
        if err != nil {
            fmt.Fprintf(os.Stderr, "Error decoding hex string %s: %s\n", args[i], err.Error())
            return nil, err
        }
    }
    fmt.Printf("line 69\tdata[0] = %x\tdata[1] = %x\tdata[2] = %x\n",data[0], data[1], data[2])
    return data, nil
}

调用这段代码并传入 args = []string{"010203","040506","070809"} 会产生以下输出:

i = 0    tmp = 010203
i = 1    tmp = 040506
i = 3    tmp = 070809
line 69 data[0] =     data[1] =     data[2] = 070809

推测该函数返回 [][]byte{[]byte{}, []byte{}, []byte{0x07, 0x08, 0x09}}

我理解这是由于Go语言的指针行为导致的;在这种情况下,如何进行深拷贝是最佳实践呢?

英文:

I would like to read a slice of strings representing hexadecimal numbers, and decode them to a slice of byte slices ([]string --&gt; [][]byte). This is my code so far:

func (self *algo_t) decode_args(args []string) ([][]byte, error) {
	var data [][]byte
	for i := uint32(0); i &lt; self.num_args; i++ {
		data = make([][]byte, self.num_args)
		tmp, err := hex.DecodeString(args[i])
		fmt.Printf(&quot;i = %d\ttmp = %x\n&quot;, i, tmp)
		data[i] = make([]byte, len(tmp))
		copy(data[i], tmp)
		if err != nil {
			fmt.Fprintf(os.Stderr, &quot;Error decoding hex string %s: %s\n&quot;, args[i], err.Error())
			return nil, err
		}
	}
	fmt.Printf(&quot;line 69\tdata[0] = %x\tdata[1] = %x\tdata[2] = %x\n&quot;,data[0], data[1], data[2])
	return data, nil
}

calling this code and passing args = []string{&quot;010203&quot;,&quot;040506&quot;,&quot;070809&quot;} yields the following output:

i = 0    tmp = 010203
i = 1    tmp = 040506
i = 3    tmp = 070809
line 69 data[0] =     data[1] =     data[2] = 070809

Presumably the function returns [][]byte{[]byte{}, []byte{}, []byte{0x07, 0x08, 0x09}}.

I understand that this is because of the pointer behavior of Go; what is the best practice for doing a deep copy of this kind?

答案1

得分: 2

例如,

package main

import (
    "encoding/hex"
    "fmt"
)

// 将十六进制字符串解码为字节数组
func decode(s []string) ([][]byte, error) {
    b := make([][]byte, len(s))
    for i, ss := range s {
        h, err := hex.DecodeString(ss)
        if err != nil {
            err = fmt.Errorf(
                "解码十六进制字符串 %s 出错:%s\n",
                ss, err.Error(),
            )
            return nil, err
        }
        b[i] = h
    }
    return b, nil
}

func main() {
    s := []string{"010203", "040506", "070809"}
    fmt.Println(s)
    b, err := decode(s)
    if err != nil {
        fmt.Println(err)
    } else {
        fmt.Println(b)
    }
    s = []string{"ABCDEF", "012345", "09AF"}
    fmt.Println(s)
    b, err = decode(s)
    if err != nil {
        fmt.Println(err)
    } else {
        fmt.Println(b)
    }
    s = []string{"01", "123XYZ"}
    fmt.Println(s)
    b, err = decode(s)
    if err != nil {
        fmt.Println(err)
    } else {
        fmt.Println(b)
    }
}

输出结果:

[010203 040506 070809]
[[1 2 3] [4 5 6] [7 8 9]]
[ABCDEF 012345 09AF]
[[171 205 239] [1 35 69] [9 175]]
[01 123XYZ]
解码十六进制字符串 123XYZ 出错:encoding/hex: invalid byte: U+0058 'X'
英文:

For example,

package main

import (
	&quot;encoding/hex&quot;
	&quot;fmt&quot;
)

// Decode hex []string to [][]byte
func decode(s []string) ([][]byte, error) {
	b := make([][]byte, len(s))
	for i, ss := range s {
		h, err := hex.DecodeString(ss)
		if err != nil {
			err = fmt.Errorf(
				&quot;Error decoding hex string %s: %s\n&quot;,
				ss, err.Error(),
			)
			return nil, err
		}
		b[i] = h
	}
	return b, nil
}

func main() {
	s := []string{&quot;010203&quot;, &quot;040506&quot;, &quot;070809&quot;}
	fmt.Println(s)
	b, err := decode(s)
	if err != nil {
		fmt.Println(err)
	} else {
		fmt.Println(b)
	}
	s = []string{&quot;ABCDEF&quot;, &quot;012345&quot;, &quot;09AF&quot;}
	fmt.Println(s)
	b, err = decode(s)
	if err != nil {
		fmt.Println(err)
	} else {
		fmt.Println(b)
	}
	s = []string{&quot;01&quot;, &quot;123XYZ&quot;}
	fmt.Println(s)
	b, err = decode(s)
	if err != nil {
		fmt.Println(err)
	} else {
		fmt.Println(b)
	}
}

Output:

[010203 040506 070809]
[[1 2 3] [4 5 6] [7 8 9]]
[ABCDEF 012345 09AF]
[[171 205 239] [1 35 69] [9 175]]
[01 123XYZ]
Error decoding hex string 123XYZ: encoding/hex: invalid byte: U+0058 &#39;X&#39;

答案2

得分: 0

有一个专门用于处理深拷贝的包:http://godoc.org/code.google.com/p/rog-go/exp/deepcopy

你可以在这里查看源代码:https://code.google.com/p/rog-go/source/browse/exp/deepcopy/deepcopy.go。它涵盖了对切片和指针的拷贝,所以应该适用于你的情况。

英文:

There is a package built specifically to handle deep copy: http://godoc.org/code.google.com/p/rog-go/exp/deepcopy

You can look at the source here: https://code.google.com/p/rog-go/source/browse/exp/deepcopy/deepcopy.go. It covers copying slices and pointers, so it should cover your case.

huangapple
  • 本文由 发表于 2014年4月24日 05:34:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/23255629.html
匿名

发表评论

匿名网友

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

确定