I have this code code in Go:
type T struct{
l list.List
}
//Global variable
var movsKnight [][]T
var movsKnightQueenWhite []T
//This function create a double slice, and in each position of the slice there is a list
func generateMovsKnight(){
movsKnight = make([][]T, 64)
for i := 0; i<64; i++ {
movsKnight[i] = make([]T,2)
movsKnight[i][0].l.PushBack(i)
var col = i/8
var fil = i%8
if(col-2 >= 0){
if(fil-1 >= 0){ movsKnight[i][1].l.PushBack(((col-2)*8)+(fil-1)) }
if(fil+1 < 8){ movsKnight[i][1].l.PushBack(((col-2)*8)+(fil+1)) }
}
if(col-1 >=0){
if(fil-2 >= 0){ movsKnight[i][1].l.PushBack(((col-1)*8)+(fil-2)) }
if(fil+2 < 8){ movsKnight[i][1].l.PushBack(((col-1)*8)+(fil+2)) }
}
if(col+1 < 8){
if(fil-2 >= 0){ movsKnight[i][1].l.PushBack(((col+1)*8)+(fil-2)) }
if(fil+2 < 8){ movsKnight[i][1].l.PushBack(((col+1)*8)+(fil+2)) }
}
if(col+2 < 8){
if(fil-1 >= 0){ movsKnight[i][1].l.PushBack(((col+2)*8)+(fil-1)) }
if(fil+1 < 8){ movsKnight[i][1].l.PushBack(((col+2)*8)+(fil+1)) }
}
}
}
//This function returns one slice of 5 positions with a list in each position
func getMovsKnight(square int)([]T){
return movsKnight[square]
}
With these functions, I create a slice[][] with a list in each position of the slice.
For example, in movsKnight[0][4] I have a list with one node. And in getMovsKnight, I get the slice in one position, for example, I get movsKnight[0], so this returns a slice of 5 positions and one list in each position.
Now:
func init(){
movsKnightQueenWhite = make([]T, 2)
//Fills movsKnight
generateMovsKnight()
movsKnightQueenWhite = getMovsKnight(0)
movsKnightQueenWhite = cutList(movsKnightQueenWhite)
}
func cutList(slice []T)([]T){
//I simplify the func in order to do it more understandable
for e := slice[0].l.Front(); e != nil; e = next {
next = e.Next()
if(borrado == true){ slice[0].l.Remove(e) }
}
return slice
}
My problem is that the function cutList is modifying the variable MovsKnight (I dont want to modify this) and it is also modifying movsKnightQueenWhite (that it is correct)
If I print the memory address of both slices (movsKnight y movsKnightQueenWhite), I see that is the same address. So, the function getMovsKnight doesnt do a copy of movsKnight and returns it. I think that cutList is returning the reference of movsKnight and, for this reason, the other methods are modifying movsKnight and they arent modifying movsKnightQueenWhite as I would like.
I would like to know how I can do to copy movsKnight and how avoid to modify the original.