Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Examples in ch1-05 are wrong #15

Closed
cuonglm opened this issue Nov 10, 2022 · 3 comments
Closed

Examples in ch1-05 are wrong #15

cuonglm opened this issue Nov 10, 2022 · 3 comments

Comments

@cuonglm
Copy link

cuonglm commented Nov 10, 2022

Describe the bug

Examples in https://zalopay-oss.github.io/go-advanced/ch1-basic/ch1-05-concurrency-parallelism.html#154-v%C3%AD-d%E1%BB%A5-goroutine uses time.Sleep for synchronization. This is wrong, because when a goroutine is scheduled to run depends on the runtime scheduler, there's no guarantee that it was run after time.Sleep was executed.

func main() {
    // sử dụng từ khoá go để tạo goroutine
    go fmt.Println("Hello from another goroutine")
    fmt.Println("Hello from main goroutine")

    // chờ 1 giây để có thể chạy được goroutine              <--- wrong
    //của hàm fmt.Println trước khi hàm main kết thúc
    time.Sleep(time.Second)
}

To Reproduce

Expected behavior

Use other primitive for goroutine synchronization like channel or sync.WaitGroup.

Screenshots

Desktop (please complete the following information):

Smartphone (please complete the following information):

Additional context

@anhldbk
Copy link
Member

anhldbk commented Nov 10, 2022

@cuonglm nice catch. Any recommendation? Pls make a PR if you can.

@cuonglm
Copy link
Author

cuonglm commented Nov 11, 2022

@anhldbk The fix is simple:

func main() {
	done := make(chan struct{})
	go func() {
		defer close(done)
		fmt.Println("Hello from another goroutine")
	}()
	fmt.Println("Hello from main goroutine")
	<-done
}

But I don't know the process of generating the documentation.

@anhldbk
Copy link
Member

anhldbk commented Nov 21, 2022

@cuonglm The issue is fixed. Thanks for reporting.

@anhldbk anhldbk closed this as completed Nov 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants