goroutine

How to understand this behavior of goroutine?

怎甘沉沦 提交于 2019-12-01 13:16:49
问题 package main import ( "fmt" "time" ) type field struct { name string } func (p *field) print() { fmt.Println(p.name) } func main() { data := []field{ {"one"},{"two"},{"three"} } for _,v := range data { go v.print() } <-time.After(1 * time.Second) } why does this code print 3 "three" instead of "one" "two" "three" in any order? 回答1: There is a data race. The code implicitly takes address of variable v when evaluating arguments to the goroutine function. Note that the call v.print() is

How do I kill a goroutine

假如想象 提交于 2019-12-01 12:56:08
I have the following setup: func startsMain (){ go main () } fun stopMain (){ //kill main } func main() { //infinite loop } I am creating cucumber steps and I need to be able to start and shut down the application. You can kill you infinite loop using select and channels! var quit chan struct{} func startLoop() { quit := make(chan struct{}) go loop() } func stopLoop() { // As mentioned by Kaedys //close(quit) // permits signalling everyone havins such a `case <-quit:` // statement to be stopped at once, which might be even better. quit <- struct{}{} } // BTW, you cannot call your function main

Go program getting deadlock

喜你入骨 提交于 2019-12-01 11:59:02
Here is my Golang program which I am playing with just to get my concepts right. When I run the program it is deadlocked I don't understand why ? Please anyone point out what is going wrong ? package main import ( "fmt" "sync" ) var wg sync.WaitGroup func main() { numOfGoRoutines := 10 wg.Add(numOfGoRoutines) ch := make(chan int, numOfGoRoutines) for i := 0; i < numOfGoRoutines; i++ { a := i go sqr(ch, a, wg) } wg.Wait() fmt.Println("After WAIT") close(ch) var res int for i := range ch { res += i } ch = nil fmt.Println("result = ", res) } func sqr(ch chan int, val int, wg sync.WaitGroup) { fmt

Go program getting deadlock

雨燕双飞 提交于 2019-12-01 11:49:20
问题 Here is my Golang program which I am playing with just to get my concepts right. When I run the program it is deadlocked I don't understand why ? Please anyone point out what is going wrong ? package main import ( "fmt" "sync" ) var wg sync.WaitGroup func main() { numOfGoRoutines := 10 wg.Add(numOfGoRoutines) ch := make(chan int, numOfGoRoutines) for i := 0; i < numOfGoRoutines; i++ { a := i go sqr(ch, a, wg) } wg.Wait() fmt.Println("After WAIT") close(ch) var res int for i := range ch { res

Shutdown “worker” go routine after buffer is empty

送分小仙女□ 提交于 2019-12-01 11:25:15
I want my go routine worker ( ProcessToDo() in the code below) to wait until all "queued" work is processed before shutting down. The worker routine has a "to do" channel (buffered), through which work is sent to it. And it has a "done" channel to tell it to start shutdown. The documentation says that the select on the channels will pick a "pseudo-random value" if more than one of the selects are met... which means the shutdown (return) is being triggered before all the buffered work is completed. In the code sample below, I want all 20 messages to print... package main import ( "time" "fmt" )

Discrepancies between Go Playground and Go on my machine?

我与影子孤独终老i 提交于 2019-12-01 10:12:23
To settle some misunderstandings I have about goroutines, I went to the Go playground and ran this code : package main import ( "fmt" ) func other(done chan bool) { done <- true go func() { for { fmt.Println("Here") } }() } func main() { fmt.Println("Hello, playground") done := make(chan bool) go other(done) <-done fmt.Println("Finished.") } As I expected, Go playground came back with an error: Process took too long . This seems to imply that the goroutine created within other runs forever. But when I run the same code on my own machine, I get this output almost instantaneously: Hello,

Shutdown “worker” go routine after buffer is empty

我怕爱的太早我们不能终老 提交于 2019-12-01 08:42:26
问题 I want my go routine worker ( ProcessToDo() in the code below) to wait until all "queued" work is processed before shutting down. The worker routine has a "to do" channel (buffered), through which work is sent to it. And it has a "done" channel to tell it to start shutdown. The documentation says that the select on the channels will pick a "pseudo-random value" if more than one of the selects are met... which means the shutdown (return) is being triggered before all the buffered work is

goroutines order of execution

泄露秘密 提交于 2019-12-01 06:21:35
I'm trying to understand this piece of code, not sure why the 2nd go is executed before the 1st one. It'd be great if someone can really help me out with this! func sum(a []int, c chan int) { fmt.Println("summing: ", a) total := 0 for _, v := range a { total += v } //fmt.Println("send to c",total) c <- total // send total to c } func main() { //a := []int{7, 2, 8,134,23,23,1,23,1234,143, -9, 4, 0, 1234} c := make(chan int) go sum([]int{1,2,3}, c) go sum([]int{4,5,6}, c) x := <-c fmt.Println(x) x = <-c fmt.Println(x) } OUTPUT: summing: [4 5 6] 15 summing: [1 2 3] 6 You have nothing explicitly

What happens to return value from goroutine

落爺英雄遲暮 提交于 2019-12-01 00:09:52
问题 Could someone please give clarification values returned from the goroutine. Does the returned value from the goroutine is getting stored on stake. example : // function getNumber returns the "int i" and we can't use this returned value // because this function is invoked as goroutine. // We know that, to communicate between main and goroutine one could // use the channel ( chan <- i), but I am interested to know about // the use of return i in goroutine. Is it possible to get/ use this //

Golang app using sync.WaitGroup & channels never exits

怎甘沉沦 提交于 2019-11-30 21:56:03
I use sync.WaitGroup , defer wg.Close() and wg.Wait() to wait for my goroutines to complete. The program do wait, but it never exits. This is my program (runnable): package main import ( "fmt" "io" "log" "net/http" "os" "sync" ) var symbols = []string{ "ASSA-B.ST", "ELUX-B.ST", "HM-B.ST", } func main() { fmt.Println("fetching quotes...") fetchedSymbols := make(chan string) var wg sync.WaitGroup wg.Add(len(symbols)) for _, symbol := range symbols { go fetchSymbol(symbol, &wg, fetchedSymbols) } for response := range fetchedSymbols { fmt.Println("fetched " + response) } wg.Wait() fmt.Println(