Last active
October 19, 2021 13:37
-
-
Save loic-roux-404/259c776433160797f0d253321d7383d5 to your computer and use it in GitHub Desktop.
Concurrent append to store ingo (experiment)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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