Hacker symbol

November 26, 2019 ~ 1 min read

Mutex and locking


import (
  "sync"
)

type Account struct {
   balance int64
   closed  bool
   sync.Mutex
}

 func (a *Account) Close() (payout int64, ok bool) {
   // This makes sure that only this function can change the contents
   a.Lock()
   // Afterthe function is done it will unlocked no matter what happens
   // in this function
   defer a.Unlock()
   fmt.Printf("")
   payout = a.balance
}

Sebastian BolaƱos

Hi, I'm Sebastian. I'm a software developer from Costa Rica. You can follow me on Twitter. I enjoy working on distributed systems.