Last active
July 13, 2025 17:36
-
-
Save scarletquasar/d4de5aa9cdbc5310c29cea2044e8c610 to your computer and use it in GitHub Desktop.
Named Goroutine
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"sync" | |
"time" | |
) | |
var ( | |
results = make(map[string]string) | |
mu sync.Mutex | |
wg sync.WaitGroup | |
) | |
func main() { | |
wg.Add(1) | |
go NamedGoroutine("test1") | |
wg.Add(1) | |
go NamedGoroutine("test2") | |
time.Sleep(100 * time.Millisecond) | |
mu.Lock() | |
res1 := results["test1"] | |
res2 := results["test2"] | |
mu.Unlock() | |
fmt.Println("Final result for test1:", res1) | |
fmt.Println("Final result for test2:", res2) | |
wg.Wait() | |
} | |
func NamedGoroutine(name string) { | |
defer wg.Done() | |
time.Sleep(50 * time.Millisecond) | |
result := "processed " + name | |
mu.Lock() | |
results[name] = result | |
mu.Unlock() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment