Skip to content

Instantly share code, notes, and snippets.

@loic-roux-404
Last active October 19, 2021 13:37
Show Gist options
  • Save loic-roux-404/259c776433160797f0d253321d7383d5 to your computer and use it in GitHub Desktop.
Save loic-roux-404/259c776433160797f0d253321d7383d5 to your computer and use it in GitHub Desktop.
Concurrent append to store ingo (experiment)
package main
import "fmt"
var store []interface{}
var c = make(chan interface{}, 2) // 2 threads
func asyncAdd(i interface{}) error {
go add(c, i)
val := <-c
store = append(store, val)
// close(c)
return nil
}
func add(c chan interface{}, i interface{}) error {
c <- i
return nil
}
func main() {
i := "jar"
asyncAdd(i)
asyncAdd("ayo")
for i, v := range store {
println(i, v)
fmt.Printf("%T\t", v)
fmt.Println(v)
}
}
/*
0 (0x486f60,0xc00009a050)
string jar
1 (0x486f60,0x4b2448)
string ayo
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment