Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

something like this would do it:

  package main
  
  import (
    "sync"
    "time"
  )
  
  type WaitGroup struct {
    sync.WaitGroup
  }
  
  func (wg *WaitGroup) Go(fn func()) {
    wg.Add(1)
    go func() {
      defer wg.Done()
      fn()
    }()
  }
  
  func main() {
    var wg WaitGroup
    wg.Go(func() { time.Sleep(1 * time.Second) })
    wg.Wait()
  }




This is really amazing, thank you so much!

It's called struct embedding, there an article about it in Go by Example if you're interested: https://gobyexample.com/struct-embedding

Thank you @listeria, today I learned about struct embedding lol!

I rediscover this about once a year and am always so happy when I do.



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: