Go 编程中的错误处理与并发机制
错误处理
在编程中,错误处理是一个重要的方面,它能帮助我们更好地应对程序运行时可能出现的问题。下面将介绍 Go 语言中几种不同的错误处理方式。
空白标识符的使用
在某些情况下,我们可能会得到一些不需要使用的返回值。这时,可以使用空白标识符(下划线 _ )来忽略这些值。例如,在以下代码中:
package main
import (
"fmt"
)
type account struct {
number string
balance float64
}
func (a *account) withdraw(value float64) (bool, error) {
if a.balance >= value {
a.balance = a.balance - value
return true, nil
}
return false, fmt.Errorf("Withdrawal failed, because the request amount of %0.2f is higher than balance of %0.2f ", value, a.balance)
}
func main() {
acct := account{}
acct.number = "C21345345345355"
acct.balance = 159
// 我们使用 _ 来忽略 out,因为我们不需要它
_, err
超级会员免费看
订阅专栏 解锁全文

被折叠的 条评论
为什么被折叠?



