Go: deep copy slices -


I would like to read a piece of strings that represent hexadecimal numbers, and by them the byte slice ( [] string - & Gt; [] [] Byte ). This is my code so far:

  Funk (self * algo_t) decode_arge (args [] string) ([] [] byte, error) {var data [] [] for byte : = UIT 32 (0); I & lt; Self.num_args; I ++ {data = create ([] [] byte, manually. NUm_argue) TMP, gal .: = hex. Decodestring (args [i]) fmt.printf ("i =% d \ ttmp =% x \ n", i, tmp) data [i] = create ([] byte, lane (tmp)) copy (data) ], TMP) If mistake! = Zero {FMT. FITTF (Oststir, "Error decoding hex string% s:% (" line 69 \ tdata [0] =% x \ tdata [1] =% x \ tdata [2] \ n ", args [i], err.Error Return data, zero}    p> This code is calling and passing  args = [] string {"010203", "040506", "070809"}  gives the following output:  
  I = 0 tmp = 010203 i = 1 tmp = 040506 i = 3 tmp = 07080 9 line 69 data [0] = data [1] = data [2] = 070809   

possible : Returns the function [] [] byte [[] byte {}, [] byte {}, [] by {0x07, 0x08, 0x09}} .

I think this is due to the indicative behavior of Go; what is the best practice to make such a deep copy? FMT ") / decode hex [] string to [] [] byte funk decode (s [] string) ([] [] byte, error) {b: = create ([] [] byte, lane (s)) For i, ss: = range s {h, err: = hex.DecodeString (ss) if err! = Nil {err = fmt.Errof ("error decoding hex string% s:% s \ n", ss, err.Error (),) return zero, err} b [i] = h} return b, nil} func main () {s: = [] string {"010203", "040506", "070809"} fmt.println (s) b, err: = decode (s) if mistake! = Zero {fmt.Println (mistake)} other {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)}}

Output:

  [010203 040506 07080 9] [[1 2 3] [4 5 6] [7 8 9]] [ABCDEF 01 2345 09 AF] [[171 205 23 9] [1 35 69] [9 175]] [01 123XYZ] Hex String 123XYZ Error Encoding / Hex: invalid byte: U + 0058 'X'    

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -