Reputation:
I am trying to figure out what is wrong with my code all this morning but couldn't. It says it cannot assign containers. Please check this go play ground http://play.golang.org/p/RQmmi7nJAK
And the problematic code is below.
func My_Merge(container []int, first_index int, mid_index int, last_index int) {
left_array := make([]int, mid_index-first_index+1)
right_array := make([]int, last_index-mid_index)
temp_i := 0
temp_j := 0
for i := first_index; i < mid_index; i++ {
left_array[temp_i] = container[i]
temp_i++
}
for j := mid_index; j < last_index+1; j++ {
right_array[temp_j] = container[j]
temp_j++
}
i := 0
j := 0
for elem := first_index; elem < len(container); elem++ {
if left_array[i] <= right_array[j] {
container[elem] = left_array[i]
i++
if i == len(left_array) {
container[elem+1:last_index] = right_array[j:]
break
}
} else {
container[elem] = right_array[j]
j++
if j == len(right_array) {
container[elem+1:last_index] = left_array[i:]
break
}
}
}
}
I am getting the errors in the line container[elem+1:last_index] = right_array[j:]. Even if I delete the whole block, I am getting errors. Could anybody help me on this? I would greatly appreciate it.
Upvotes: 0
Views: 133
Reputation: 16120
You can't assign to a slice expression in Go. You need to use copy:
copy(container[elem+1:last_index], right_array[j:])
But apparently there are other problems too, since when I change that in the playground I get an index out of range error.
Upvotes: 1