package main

import (
	"fmt"
	"sync"
	"sync/atomic"
)

type Account struct {
	balance int32
}

func transfer(amount int32, from *Account, to *Account, group *sync.WaitGroup) {

	if from.balance >= amount {
		atomic.AddInt32(&(from.balance), -amount)
		atomic.AddInt32(&(to.balance), amount)
	}
	group.Done()
}

func main() {

	account1 := Account{100}
	account2 := Account{100}

	var group sync.WaitGroup

	for i := 0; i < 100; i++ {
		group.Add(2)
		go transfer(1, &account1, &account2, &group)
		go transfer(1, &account2, &account1, &group)
	}
	group.Wait()
	fmt.Println("Account1 Balance = $", account1.balance)
	fmt.Println("Account2 Balance = $", account2.balance)
}
