Reputation: 458
My application has a slice of a struct like so
type ItemOrder struct {
ItemId
...
}
var items = []*ItemOrder
And a variadic function accepting ...int
func ItemIds(lang string, ids ...int){
...
How can I take all itemIds from the items []*ItemOrder
slice and supply it to the variadic function? Something like
itemsPB, err := ItemIds("", items[:].itemId)
Abov doesnt work because im not giving the slice a position to extract itemId from.
Upvotes: 2
Views: 436
Reputation: 417412
You have to create a new slice for the IDs, and use a loop to populate it. There is no shortcut.
For example:
ids := make([]int, len(items))
for i, item := range items {
ids[i] = item.ItemId
}
ItemIds("en", ids...)
Try it on the Go Playground.
Upvotes: 6