Partial Slice Swapping


(Sayan Shankhari) #1

Hello Masters,
This is from a beginner in go. Please read the code for Swapping a 64 element array using two 32 element arrays. I have used two ways (OPTIONs), one is working, the other is not. Please tell me where I am wrong and what is the real concept in this language.
Thank You.

package main

import "fmt"

func main() {
	var i int

	X := make([]byte, 64)
	for i = 0; i < 32; i++ {
		X[i] = 0; X[32+i] = 1;
	}
fmt.Println(X)

//OPTION_1--------------------------------------------------
	L := make([]byte, 32)
	R := make([]byte, 32)
	for i = 0; i < 32; i++ {
		L[i] = X[i]; R[i] = X[32+i];
	}
//OPTION_2--------------------------------------------------
//	L := X[0:32]
//	R := X[32:64]
//--------------------------------------------------
fmt.Println(L)
fmt.Println(R)

//COPYING--------------------------------------------------
/*
	for i = 0; i < 32; i++ {
		X[i] = R[i]
		X[32+i] = L[i]
	}
*/
copy(X[0:32], R[:])
copy(X[32:64], L[:])

fmt.Println(X)
}