Sha256: dfe4136b332e56699551e10c4405d6ddebac0518a43bd65fdbfbe1ca94a14853

Contents?: true

Size: 786 Bytes

Versions: 210

Compression:

Stored size: 786 Bytes

Contents

package account

import "sync"

type Account struct {
	sync.RWMutex
	open    bool
	balance int64
}

func Open(amt int64) *Account {
	if amt < 0 {
		return nil
	}
	return &Account{open: true, balance: amt}
}

func (a *Account) Balance() (bal int64, ok bool) {
	a.RLock()
	bal, ok = a.balance, a.open
	a.RUnlock()
	return
}

func (a *Account) Deposit(amt int64) (newBal int64, ok bool) {
	a.Lock()
	defer a.Unlock()
	if !a.open {
		return a.balance, false
	}
	if amt < 0 && a.balance+amt < 0 {
		return a.balance, false
	}
	a.balance += amt
	return a.balance, true
}

func (a *Account) Close() (pay int64, ok bool) {
	a.Lock()
	defer a.Unlock()
	if !a.open {
		return 0, false
	}
	a.open = false
	if a.balance < 0 {
		return 0, true
	}
	pay = a.balance
	a.balance = 0
	return pay, true
}

Version data entries

210 entries across 210 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/go/exercises/bank-account/example.go
trackler-2.2.1.179 tracks/go/exercises/bank-account/example.go
trackler-2.2.1.178 tracks/go/exercises/bank-account/example.go
trackler-2.2.1.177 tracks/go/exercises/bank-account/example.go
trackler-2.2.1.176 tracks/go/exercises/bank-account/example.go
trackler-2.2.1.175 tracks/go/exercises/bank-account/example.go
trackler-2.2.1.174 tracks/go/exercises/bank-account/example.go
trackler-2.2.1.173 tracks/go/exercises/bank-account/example.go
trackler-2.2.1.172 tracks/go/exercises/bank-account/example.go
trackler-2.2.1.171 tracks/go/exercises/bank-account/example.go
trackler-2.2.1.170 tracks/go/exercises/bank-account/example.go
trackler-2.2.1.169 tracks/go/exercises/bank-account/example.go
trackler-2.2.1.167 tracks/go/exercises/bank-account/example.go
trackler-2.2.1.166 tracks/go/exercises/bank-account/example.go
trackler-2.2.1.165 tracks/go/exercises/bank-account/example.go
trackler-2.2.1.164 tracks/go/exercises/bank-account/example.go
trackler-2.2.1.163 tracks/go/exercises/bank-account/example.go
trackler-2.2.1.162 tracks/go/exercises/bank-account/example.go
trackler-2.2.1.161 tracks/go/exercises/bank-account/example.go
trackler-2.2.1.160 tracks/go/exercises/bank-account/example.go