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
EDIT: I initially didn’t include all the code by accident
package account
import (
"sync"
)
// Define the Account type here.
type Account struct {
sync.RWMutex
balance int64
active bool
}
func Open(amount int64) *Account {
if amount < 0 {
return nil
}
return &Account{
balance: amount,
active: true,
}
}
// Balance returns the balance of the account
func (a *Account) Balance() (int64, bool) {
a.RLock()
defer a.RUnlock()
// fail early in lieu of returning invalid state on API
if a.balance > 0 && !a.active {
panic("an inactive account has nonzero balance")
} else if a.balance < 0 {
panic("account found with negative balance")
}
return a.balance, a.active
}
func (a *Account) Deposit(amount int64) (int64, bool) {
a.Lock()
defer a.Unlock()
if !a.active || a.balance+amount < 0 {
return a.balance, false
} else {
a.balance += amount
return a.balance, true
}
}
func (a *Account) Close() (int64, bool) {
a.Lock()
defer func() {
if !a.active {
a.balance = 0
}
a.Unlock()
}()
if !a.active {
return a.balance, false
} else {
a.active = false
return a.balance, true
}
}