Hi 

Here is the code:

Version 1:
package main

import (
"fmt"
"sync"
)

var a string
var once sync.Once

func setup() {
a = "hello,world\n"
}
func doprint() {
once.Do(setup)
fmt.Print(a)
}
func twoprint() <-chan struct{} {
var wg sync.WaitGroup
wg.Add(2)
ch := make(chan struct{})

go func() {
doprint()
wg.Done()
}()
go func() {
doprint()
wg.Done()
}()

go func() {
wg.Wait()
close(ch)
}()

return ch
}

func main() {
ch := twoprint()
<-ch
}


Version 2:
package main

import (
"fmt"
"sync"
)

var a string
var once sync.Once

func setup() {
a = "hello,world\n"
}
func doprint() {
once.Do(setup)
fmt.Print(a)
}
func twoprint() {
var wg sync.WaitGroup
wg.Add(2)

go func() {
doprint()
wg.Done()
}()
go func() {
doprint()
wg.Done()
}()

wg.Wait()
}

func main() {
twoprint()
}


Cheers snmed


Am Donnerstag, 2. November 2017 10:37:15 UTC+1 schrieb 28911...@gmail.com:
>
> Sorry,I try my best to open the website but it can't work.Can you write it 
> ??Thank you so much.
>
> 在 2017年10月30日星期一 UTC+8下午4:29:44,snmed写道:
>>
>> Hi
>>
>> There are several ways to solve it, here are two of them:
>>
>> https://play.golang.org/p/wJwkI7HQwv
>> https://play.golang.org/p/nasUcgBeG4
>>
>> I prefer the first one, because so I can decide if i want to wait for the 
>> end of twoprint or not.
>>
>> Cheers
>>
>> Am Montag, 30. Oktober 2017 06:43:45 UTC+1 schrieb 28911...@gmail.com:
>>>
>>> Yes, you're right.So how to solve it??
>>>
>>> 在 2017年10月30日星期一 UTC+8下午12:37:49,Dave Cheney写道:
>>>>
>>>> Hello. I’m guessing that your tried calling twoprint(), but you’ve 
>>>> probably found that nothing is printed to the screen before your program 
>>>> exits. 
>>>>
>>>> Is that correct?
>>>
>>>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to