Reputation: 113
I'm working on a client for a specific API, and I built several structures (one for each route).
The API works with pages so I have getters returning slices of a struct. This is done passing through a common function that takes an interface supposed to be a pointer to a slice of the wanted structure so I can use Unmarshal
from the json
package.
The problem I'm facing is that I would like to get all the pages in a single request, but in order to do that I would need to concatenate several slices.
I tried to do this using reflect
but I can only manage to create a new slice without actually altering the value that my interface is pointing to.
Here's a part of my code:
func concatenateSlice(dst, src interface{})(){
dstValue := reflect.ValueOf(dst)
srcValue := reflect.ValueOf(src)
if srcValue.Type() != dstValue.Type(){
return
}
switch srcValue.Kind(){
case reflect.Ptr, reflect.Slice:
dstValuePoint := reflect.Indirect(dstValue)
srcValuePoint := reflect.Indirect(srcValue)
reflect.Copy(dstValuePoint, reflect.AppendSlice(dstValuePoint, srcValuePoint))
}
}
I'm sorry for the sloppy post, this is my first time on stackoverflow.
Upvotes: 2
Views: 192
Reputation: 417512
You shouldn't use reflect.Copy()
to copy the result slice (after append), as destination will not have enough space. reflect.AppendSlice()
will not change the slice pointed by dst
, it will allocate a new one if needed, and it returns the new slice (more precisely the reflect.Value
holding it).
What you should do is set the slice returned by reflect.AppendSlice()
to the value pointed by dst
, simply using the Value.Set()
method. One thing to note is that you are not setting a slice pointer but a slice, so first call e.g. Value.Elem()
before calling Set()
.
See this example:
func concatenateSlice(dst, src interface{}) {
dv := reflect.ValueOf(dst)
sv := reflect.ValueOf(src)
if dv.Type() != sv.Type() {
return
}
dv2 := reflect.AppendSlice(dv.Elem(), sv.Elem())
dv.Elem().Set(dv2)
}
Testing it:
dst := []int{1, 2}
src := []int{3, 4}
concatenateSlice(&dst, &src)
fmt.Println(dst)
Output (try it on the Go Playground):
[1 2 3 4]
Upvotes: 1