Reputation: 2117
I'm trying to implement a simple merge sort using an auxiliary array. I have type byString []string
that implements the Less, Swap, and Len
methods. It's basically following Go's sort
package's interface.
However, I'm having some difficulty choosing the best route to copy the byString
slice to a temporary array.
Please help me break out of Java's polymorphism world to make it work with Go.
func merge(data Interface, lo, mid, hi int) {
i, j := lo, mid+1
// How do I copy data's elements to a new slice called aux?
}
Upvotes: 1
Views: 1418
Reputation: 12256
Use the built-in copy
function, you just have to declare the new slice as an interface type:
type Interface []string
func merge(data Interface, lo, mid, hi int) {
i, j := lo, mid+1
var aux Interface = make([]string, len(data), len(data))
copy(aux, data)
}
Upvotes: 1