mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2024-11-05 05:53:45 +00:00
31 lines
462 B
Go
31 lines
462 B
Go
// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.
|
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
|
|
|
// See page 263.
|
|
|
|
// Package bank provides a concurrency-safe single-account bank.
|
|
package bank
|
|
|
|
//!+
|
|
import "sync"
|
|
|
|
var (
|
|
mu sync.Mutex // guards balance
|
|
balance int
|
|
)
|
|
|
|
func Deposit(amount int) {
|
|
mu.Lock()
|
|
balance = balance + amount
|
|
mu.Unlock()
|
|
}
|
|
|
|
func Balance() int {
|
|
mu.Lock()
|
|
b := balance
|
|
mu.Unlock()
|
|
return b
|
|
}
|
|
|
|
//!-
|