-
-
Notifications
You must be signed in to change notification settings - Fork 18
Open
Description
local go version: go1.20.6 linux/amd64
exercise: Bank Account in Go
I may have found an infrastructure bug.
My tests timed out on the server, but couldn't find where I was messing up even when comparing to community solutions. I requested mentoring, and bernot-dev replied and mentioned that he thinks it's an issue with infrastructure. We both run it and pass tests locally.
Let me know what other information I may provide.
Unsure if this will give you access, Direct mentoring link to my attempt on this exercise, with source copied below.
my code, copied from iteration 5
package account
import (
"sync"
)
// Define the Account type here.
type Account struct {
balance int64
active bool
mu sync.RWMutex
}
func Open(amount int64) *Account {
if amount < 0 {
return nil
}
return &Account{
balance: amount,
active: true,
mu: sync.RWMutex{},
}
}
// Balance returns the balance of the account
func (a *Account) Balance() (int64, bool) {
a.mu.RLock()
defer a.mu.RUnlock()
b, ok := a.balance, a.active
// fail early in lieu of returning invalid state on API
if b > 0 && !ok {
panic("an inactive account has nonzero balance")
} else if b < 0 {
panic("account found with negative balance")
}
return b, ok
}
func (a *Account) Deposit(amount int64) (int64, bool) {
a.mu.Lock()
defer a.mu.Unlock()
b, ok := a.balance, a.active
if !ok || b+amount < 0 {
return b, false
}
b += amount
a.balance = b
return b, true
}
func (a *Account) Close() (int64, bool) {
a.mu.Lock()
defer a.mu.Unlock()
b, ok := a.balance, a.active
if !ok {
return 0, false
}
a.balance, a.active = 0, false
return b, true
}Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels