这是代码 config/config.goconst PROJECT_ID = "projectid"var Ctx = context.Background()var DSClient *datastore.Clienttype Logger struct { Pkg string}func (l *Logger) Error(v ...interface{}) { log.Print("Error|", l.Pkg, ": ", v)}func (l *Logger) Info(v ...interface{}) { log.Print("Info|", l.Pkg, ": ", v)}var propFile = "${env}.properties"var Props *properties.Propertiesfunc init(){ Props = properties.MustLoadFile(propFile, properties.UTF8) DSClient, err := datastore.NewClient(Ctx, PROJECT_ID) if err != nil { log.Fatal("Couldn't connect to DataStore: ", err) } log.Print("DataStore Client: ", DSClient)}索引/index.go import ( ... "github.com/shwetanka19/project/internal/config") urs := [] user.User{} q := datastore.NewQuery("users").Filter("email=", greq.Email).Limit(1) logger.Info("Now Client: ", config.DSClient) keys, err := config.DSClient.GetAll(config.Ctx, q, &urs)日志DataStore Client: &{0xc000010840 0xc000119d40 projectid}[Now Client: <nil>]为什么 Client 已经初始化了,现在却是 nil 呢?API 调用期间会调用 index.go 中的代码。而 config 在服务器启动时被调用。
1 回答
紫衣仙女
TA贡献1839条经验 获得超15个赞
当你这样做时
DSClient, err := datastore.NewClient(Ctx, PROJECT_ID)
您创建一个新变量DSClient
来隐藏另一个变量。您只需要将其更改为:
var err error DSClient, err = datastore.NewClient(Ctx, PROJECT_ID)
- 1 回答
- 0 关注
- 111 浏览
添加回答
举报
0/150
提交
取消