在群里突然看到一段代码:
package main
import (
"fmt"
)
var DomainId int
func init() {
DomainId, err := GetDomainId()
if err != nil {
DomainId = -1
}
fmt.Println(DomainId)
}
func GetDomainId() (int, error) {
DomainId = 1
return DomainId, nil
}
注意看红色背景的一条,理论上这段代码没有错,但事实上会报错了。
[上海]Asta谢() 22:29:50
我知道
[上海]Asta谢() 22:29:55
我踩过这个坑
[上海]Asta谢() 22:30:04
init里面不能用:=
所以,上面的代码应该是写成:
func init() {
var err error
DomainId, err = GetDomainId()
if err != nil {
DomainId=-1
}
}
对比两段红色背景的代码。主要是做个笔记 .怕会忘 .
Asta谢 是谁?看这里:https://github.com/astaxie/build-web-application-with-golang ,这里有一篇他的教程,适合广大人民群众查看

评论 (0)
暂无评论