Grokking Concurrency in Go
0% completed
Once, OnceFunc, and OnceValue
A service reads its configuration from a file, and reading and parsing that file takes 100 ms. Loading it when the program starts would slow every start, including runs that never need it, so the service loads it the first time some code asks. Now ten goroutines ask at the same moment. Which of them should load the file, and what should the other nine do while it loads?
A plain if config == nil check fails here. Several goroutines can see nil before any of them finishes loading, so the file is read several times, and some goroutines may read a half-written value
.....
.....
.....
Like the course? Get enrolled and start learning!
Reading Progress
0%