Skip to content

Commit

Permalink
add go experience, for data race
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel Sun committed Jan 23, 2024
1 parent ac48892 commit be52872
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions _posts/2024-01-09-go-experience.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
layout: post
title: "go experiences"
date: 2024-01-11 12:25:06
description: go experiences
tags:
- code
---

- data race
[refer](https://www.sohamkamani.com/golang/data-races/?utm_content=cmp-true)
We don't know which goroutine(main or i=5) will be executed firstly
```
func main() {
fmt.Println(getNumber())
}
func getNumber() int {
var i int
go func() {
i = 5
}()
return i
}
- adding waitgroups
func getNumber() int {
var i int
// Initialize a waitgroup variable
var wg sync.WaitGroup
// `Add(1) signifies that there is 1 task that we need to wait for
wg.Add(1)
go func() {
i = 5
// Calling `wg.Done` indicates that we are done with the task we are waiting fo
wg.Done()
}()
// `wg.Wait` blocks until `wg.Done` is called the same number of times
// as the amount of tasks we have (in this case, 1 time)
wg.Wait()
return i
}
```

0 comments on commit be52872

Please sign in to comment.