I have a library function that returns a slice of (very simple) structs defined in that library:
// pkg1
type Label struct {
Name, Value string
}
type Labels []Label
func Get() Labels { ... }
Another unrelated library defines an identical struct and a function that takes a slice of those structs:
// pkg2
type Label struct {
Name string
Value string
}
func Apply(labels []Label) []Label { ... }
As far as I understand, there are conditions under which conversions between identical underlying types are allowed in Go (https://stackoverflow.com/questions/24613271/golang-is-conversion-between-different-struct-types-possible), but those rules don't seem to apply to this specific situation:
// I tried to make every conversion step explicit here
var oldLabels, newLabels []pkg1.Label
var vmOldLabels, vmNewLabels []pkg2.Label
var finalLabels pkg1.Labels
oldLabels = pkg1.Get()
vmOldLabels = ([]pkg2.Label)(oldLabels)
vmNewLabels = pkg2.Apply(vmOldLabels)
newLabels = ([]pkg1.Label)(vmNewLabels)
finalLabels = newLabels
path/to/file.go:383:41: cannot convert oldLabels (variable of type []pkg1.Label) to type []pkg2.Label
path/to/file.go:385:32: cannot convert vmNewLabels (variable of type []pkg2.Label) to type []pkg1.Label
Is it somehow possible to convert between those two slice types without copying? If not, how do I do that with the least amount of copying?