go - 不能调用非函数撤回(类型 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)。
解决方案
它的意思正是它所说的:你不能调用非函数。只能调用函数。
这里:
deposit(deposit)
您正在尝试调用deposit
,但deposit
类型为int
:
var deposit int
这个变量声明deposit
在包范围内隐藏了同名的函数。
要解决您的问题,请为变量或函数使用不同的名称。
推荐阅读
- angular - 在Angular中切换子组件进出组件
- class - 在类方法上使用 apply()
- javascript - AJAX 和 PHP - 检查直到变量等于是
- c++ - 是否使用定义的任意、文字、非零值初始化指针?
- android - 无法为 Firebase 存储上传任务添加 OnSuccessListener
- google-chrome - 堆快照统计解释
- node.js - firestore子集合添加nodejs不起作用
- java - 保存时休眠超时
- python - LibreOffice Calc 上的 Python UNO,重新定位光标
- arrays - MIPS 从数组中读取整数