Senior
Can you give an example of a non-trivial implementation that you have personally done?
sobes.tech AI
Answer from AI
One of the non-trivial implementations I have done in Go is a custom goroutine pool with a limit on the number of tasks running simultaneously and the ability to scale dynamically.
The idea was to efficiently manage a large number of asynchronous tasks without creating too many goroutines, which could lead to excessive memory consumption and reduced performance.
Here's a simplified example of a pool implementation:
package main
import (
"fmt"
"sync"
)
type Task func()
type Pool struct {
tasks chan Task
wg sync.WaitGroup
}
func NewPool(workerCount int) *Pool {
p := &Pool{
tasks: make(chan Task),
}
p.wg.Add(workerCount)
for i := 0; i < workerCount; i++ {
go func() {
defer p.wg.Done()
for task := range p.tasks {
task()
}
}()
}
return p
}
func (p *Pool) Submit(task Task) {
p.tasks <- task
}
func (p *Pool) Shutdown() {
close(p.tasks)
p.wg.Wait()
}
func main() {
pool := NewPool(5)
for i := 0; i < 10; i++ {
n := i
pool.Submit(func() {
fmt.Printf("Task %d is running\n", n)
})
}
pool.Shutdown()
}
This approach allows controlling parallelism and avoiding the creation of too many goroutines, which is useful when working with limited resources or external APIs.