首页 > 解决方案 > 不能调用非函数撤回(类型 int)是什么意思?

问题描述

我创建了这个程序

package main

import "fmt"

var bankAccount = 12342

func withdraw(withdrawAmnt int) int { //withdraw function, return int
    bankAccount -= withdrawAmnt
    if bankAccount < 0 {
        bankAccount += withdrawAmnt //bankaccount cannot go negative
        return 1
    } else { return 0 }
}

func deposit(depositAmnt int) {
    bankAccount += depositAmnt
}

func main() {
    var choice int
    var withdraw int
    var deposit int
    fmt.Printf("%d \n 1. withdraw \n 2. deposit \n 3. exit")
    fmt.Scanln(&choice)
    switch choice {
        case 1:
            fmt.Scanln(&withdraw)
            if withdraw(withdraw) != 0 {
                fmt.Println("Not succesful: not enough money (you're broke)")
            }
        case 2:
            fmt.Scanln(&deposit)
            deposit(deposit)
        case 3:
            os.Exit()
    }
}

我不断收到此错误:无法调用非函数存款(类型 int)并且无法调用非函数存款(类型 int)。

标签: go

解决方案


它的意思正是它所说的:你不能调用非函数。只能调用函数。

这里:

            deposit(deposit)

您正在尝试调用deposit,但deposit类型为int

    var deposit int

这个变量声明deposit在包范围内隐藏了同名的函数。

要解决您的问题,请为变量或函数使用不同的名称。


推荐阅读