How to pass growing slice to go routines for access to latest values?

(Robert Towne) #1

It appears to be frowned upon to pass a pointer to a slice to a function, but I have an example where I want to pass a slice to a goroutine and it sees the new values that other goroutines add over time (existing values don’t change - just appends). The best I have seen is to create a type mimic’ing the slice and use pointers to that - but it feels this may even be frowned upon. If the correct way to handle this to pass a channel into the goroutine that maintains the appending to a local copy of the slice or is there something simpler that I can utilize?

For example:

type Foo []int

func main() {
	foo := Foo{1}
	go func(foo *Foo) {
		//	do something using foo as its values grow slowly over time

Thanks for any feedback / opportunity for me to learn :slight_smile: