mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2025-09-13 07:02:16 +00:00
good good study, day day up!
This commit is contained in:
30
vendor/gopl.io/ch9/bank3/bank.go
generated
vendored
Normal file
30
vendor/gopl.io/ch9/bank3/bank.go
generated
vendored
Normal file
@@ -0,0 +1,30 @@
|
||||
// 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
|
||||
}
|
||||
|
||||
//!-
|
Reference in New Issue
Block a user