等待一个函数在golang中执行完毕。

huangapple go评论125阅读模式
英文:

Wait for a function to finish in golang

问题

我可以帮你翻译代码部分,以下是你提供的代码的中文翻译:

  1. func A(){
  2. go print("hello")
  3. }
  4. func main() {
  5. A()
  6. // 在这里,我想要等待打印完成后再执行B()
  7. B()
  8. }

你想知道如何确保只有在打印完成后才执行B()函数。

英文:

I have the following code in golang:

  1. func A(){
  2. go print("hello")
  3. }
  4. func main() {
  5. A()
  6. // here I want to wait for the print to happen
  7. B()
  8. }

How can I somehow ensure that B() will be executed only after the print has happened?

答案1

得分: 1

使用sync.Mutex:

  1. var l sync.Mutex
  2. func A() {
  3. go func() {
  4. print("你好")
  5. l.Unlock()
  6. }()
  7. }
  8. func B() {
  9. print("世界")
  10. }
  11. func TestLock(t *testing.T) {
  12. l.Lock()
  13. A()
  14. l.Lock()
  15. // 在这里我想要等待打印完成
  16. B()
  17. l.Unlock()
  18. }

使用sync.WaitGroup:

  1. var wg sync.WaitGroup
  2. func A() {
  3. go func() {
  4. print("你好")
  5. wg.Done()
  6. }()
  7. }
  8. func B() {
  9. print("世界")
  10. }
  11. func TestLock(t *testing.T) {
  12. wg.Add(1)
  13. A()
  14. wg.Wait()
  15. // 在这里我想要等待打印完成
  16. B()
  17. }

使用chan:

  1. func A() chan struct{} {
  2. c := make(chan struct{})
  3. go func() {
  4. print("你好")
  5. c <- struct{}{}
  6. }()
  7. return c
  8. }
  9. func B() {
  10. print("世界")
  11. }
  12. func TestLock(t *testing.T) {
  13. c := A()
  14. // 在这里我想要等待打印完成
  15. <-c
  16. B()
  17. }
英文:

Use sync.Mutex

  1. var l sync.Mutex
  2. func A() {
  3. go func() {
  4. print(&quot;hello&quot;)
  5. l.Unlock()
  6. }()
  7. }
  8. func B() {
  9. print(&quot;world&quot;)
  10. }
  11. func TestLock(t *testing.T) {
  12. l.Lock()
  13. A()
  14. l.Lock()
  15. // here I want to wait for the print to happen
  16. B()
  17. l.Unlock()
  18. }

Use sync.WaitGroup

  1. var wg sync.WaitGroup
  2. func A() {
  3. go func() {
  4. print(&quot;hello&quot;)
  5. wg.Done()
  6. }()
  7. }
  8. func B() {
  9. print(&quot;world&quot;)
  10. }
  11. func TestLock(t *testing.T) {
  12. wg.Add(1)
  13. A()
  14. wg.Wait()
  15. // here I want to wait for the print to happen
  16. B()
  17. }

Use chan

  1. func A() chan struct{} {
  2. c := make(chan struct{})
  3. go func() {
  4. print(&quot;hello&quot;)
  5. c &lt;- struct{}{}
  6. }()
  7. return c
  8. }
  9. func B() {
  10. print(&quot;world&quot;)
  11. }
  12. func TestLock(t *testing.T) {
  13. c := A()
  14. // here I want to wait for the print to happen
  15. &lt;-c
  16. B()
  17. }

答案2

得分: 0

你可以选择以下两种方式:

1)将A()函数改为同步执行:

  1. func A(){
  2. print("hello") // 去掉go关键字
  3. }

这样,你可以确保在A()函数返回后,print()函数已经执行完毕。

2)等待print()函数执行完毕后再进行下一步操作。

使用一个简单的通道:

  1. func A() chan struct{}{
  2. c := make(chan struct{})
  3. go func(){
  4. print("hello")
  5. close(c)
  6. }
  7. return c
  8. }
  9. func main() {
  10. c := A()
  11. // 在这里等待print()函数执行完毕
  12. <-c
  13. B()
  14. }

你也可以使用sync.Waitgroupx/sync.Errgroup来实现。

英文:

You could either

  1. make A() synchronous
  1. func A(){
  2. print(&quot;hello&quot;) // go keyword removed
  3. }

This way you are sure that print() has been executed after A returns

  1. Wait for something that only happens after the print() execution

With a simple channel:

  1. func A() chan struct{}{
  2. c := make(chan struct{})
  3. go func(){
  4. print(&quot;hello&quot;)
  5. close(c)
  6. }
  7. return c
  8. }
  9. func main() {
  10. c := A()
  11. // here I want to wait for the print to happen
  12. &lt;-c
  13. B()
  14. }

You could also use a sync.Waitgroup or x/sync.Errgroup

huangapple
  • 本文由 发表于 2023年3月1日 17:00:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75601491.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定