Original post

I am trying to append a structure loadoutT to a slice loadoutList, and I realized that slice to slice append() appends a pointer to the original memory. I really want append to take the current values in loadoutT and stick them into loadOutList one at a time:

OneUp is a recursive function that returns the next iteration of the loadout, which it generates on the fly and places in the original Loadout. The allocations are in the code below

func deepCopy(dst []loadOutT, src loadOutT) []loadOutT {
tmp := append([]loadOutT(nil), src)
dst = append(dst, tmp[0])
return dst

func getBoosterLoadoutList(config configT, boosters []boosterT) []loadOutT {
var Loadout = loadOutT{
slots: config.ShieldBoosterCount,
utilitySlots: make([]int, config.ShieldBoosterCount),
var LoadoutList []loadOutT // this will contain ~ 400,000 loadouts in time
var ShieldBoosterVariants = len(boosters)
for run := true; run; run = !(Loadout.utilitySlots[config.ShieldBoosterCount-1] == (ShieldBoosterVariants - 1)) {
Loadout = oneUp(Loadout, config.ShieldBoosterCount, ShieldBoosterVariants, 0)
LoadoutList = deepCopy(LoadoutList, Loadout)

return LoadoutList

I bet I’m doing something really utterly basically wrong, and if only I understood slices better, I would not have this issue. The loadoutlist will eventually contain up to 400,000 loadouts. At the moment, every single one of them is a copy of the original LoadOut defined at the start of the getBoosterLoadout function. This is not my desired behavior.

Help this newbie!

submitted by /u/vanderaj
[link] [comments]