-1

I have a question about Go Lang program. In this exercise, I try to pass the variable balance which initially $20,000 to deposit method with adding 3,000, then pass to withdraw method with subtract 2,500. However, the final balance is still 20,000. I do not know how to pass a variable in one method to another one, so I can get the final balance which supposed to be $20,500. Please help! Thank you so much in advance.

package main

import (
    "fmt"
    "time"
)

type Account struct {
    id int 
    balance float64
    annual_interest_rate float64
}

func (account Account) deposit(deposit1 float64) {
    account.balance = account.balance - deposit1

}

func (account Account) withdraw(withdraw1 float64) {
    account.balance = account.balance - withdraw1
}

func (account Account) getBalance() float64 {

    return account.balance
}

func (account Account) getMonthlyInterest() float64 {

    var monthlyInterestRate = account.annual_interest_rate / 1200
    var monthly_interest = account.balance * monthlyInterestRate

    return monthly_interest
}

func (account Account) getDateCreated() string {

    var output string = ""

    currentTime := time.Now()

    output = currentTime.Format("Monday January,01 2006 15:04:15 PM")

    return output
}
func main() {

    var account = Account {id:1122, balance:20000, annual_interest_rate:4.5}

    account.deposit(3000)
    account.withdraw(2500)

    fmt.Printf("Balance: $%.2f\n", account.getBalance())
    fmt.Printf("Monthly Interest: $%.2f\n", account.getMonthlyInterest())
    fmt.Printf("Date Created: %s\n", account.getDateCreated())
}
  • `func (account Account)` should be `func (account *Account)` so that the method has a "pointer receiver" and can thus modify the original account at that address, not a copy. Same code will work. (https://go.dev/play/p/XXAqXdGte5W) . You also have a bug at `account.balance - deposit1`, should be `+` not `-` (unless your meaning of "deposit" differs from mine) – erik258 Feb 25 '22 at 02:35

2 Answers2

0

Methods are passed a copy of the receiver. So if you use Account they work with a copy of the Account (and when they return the original is unchanged); you can solve this by using pointers. This issue is well covered in the tour of go which states:

Methods with pointer receivers can modify the value to which the receiver points (as Scale does here). Since methods often need to modify their receiver, pointer receivers are more common than value receivers.

So the change to your code is pretty simple e.g.

func (account *Account) deposit(deposit1 float64) {
    account.balance = account.balance - deposit1

}

See the playground for an example with all functions changed.

Brits
  • 14,829
  • 2
  • 18
  • 31
  • You may also find [this answer](https://stackoverflow.com/a/27775558/11810946) helps you work out when to use pointer receivers (and when not). – Brits Feb 25 '22 at 02:45
0

What you should do is just change your func receiver from

func (account Account) getDateCreated() string {

to

func (account *Account) getDateCreated() string {

for more details, you can reference to https://go.dev/tour/methods/8.

As Brits descriped, golang will pass a new copy of original Account when using value receiver func (account Account), you can verify it by doing this:

func (account Account) getDateCreated() string {
    println(&account) // print the Account address in memory, you will find that incoming account address is different from original account declared in `main` method
...
}
Liu Hao
  • 511
  • 5
  • 10