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.139 tracks/go/exercises/bank-account/example.go
trackler-2.2.1.138 tracks/go/exercises/bank-account/example.go
trackler-2.2.1.137 tracks/go/exercises/bank-account/example.go
trackler-2.2.1.136 tracks/go/exercises/bank-account/example.go
trackler-2.2.1.135 tracks/go/exercises/bank-account/example.go
trackler-2.2.1.134 tracks/go/exercises/bank-account/example.go
trackler-2.2.1.133 tracks/go/exercises/bank-account/example.go
trackler-2.2.1.132 tracks/go/exercises/bank-account/example.go
trackler-2.2.1.131 tracks/go/exercises/bank-account/example.go
trackler-2.2.1.130 tracks/go/exercises/bank-account/example.go
trackler-2.2.1.129 tracks/go/exercises/bank-account/example.go
trackler-2.2.1.128 tracks/go/exercises/bank-account/example.go
trackler-2.2.1.127 tracks/go/exercises/bank-account/example.go
trackler-2.2.1.126 tracks/go/exercises/bank-account/example.go
trackler-2.2.1.125 tracks/go/exercises/bank-account/example.go
trackler-2.2.1.124 tracks/go/exercises/bank-account/example.go
trackler-2.2.1.123 tracks/go/exercises/bank-account/example.go
trackler-2.2.1.122 tracks/go/exercises/bank-account/example.go
trackler-2.2.1.121 tracks/go/exercises/bank-account/example.go
trackler-2.2.1.120 tracks/go/exercises/bank-account/example.go