预期的声明,找到"IDENT"项 [英] expected declaration, found 'IDENT' item

查看:97
本文介绍了预期的声明,找到"IDENT"项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Memcache Go API编写一小段代码,以获取存储在其键之一中的数据.以下是我使用过的几行代码(从使用App引擎文档获取了代码)

Im writing a small code using Memcache Go API to Get data stored in one of its keys . Here are few of lines of code i used ( got the code from Go app-engine docs )

import "appengine/memcache"

item := &memcache.Item {
Key:   "lyric",
Value: []byte("Oh, give me a home"),
}

但是第2行给了我一个编译错误 预期的声明,找到了'IDENT'项目"

But the line 2 gives me a compilation error "expected declaration, found 'IDENT' item"

我是Go的新手,无法解决问题

I'm new to Go , not able to figure out the problem

推荐答案

:= 简短变量声明只能在函数内部使用.

The := Short variable declaration can only be used inside functions.

因此可以将 item 变量声明放在这样的函数中:

So either put the item variable declaration inside a function like this:

import "appengine/memcache"

func MyFunc() {
    item := &memcache.Item {
        Key:   "lyric",
        Value: []byte("Oh, give me a home"),
    }
    // do something with item
}

或将其设为全局变量并使用 var 关键字:

Or make it a global variable and use the var keyword:

import "appengine/memcache"

var item = &memcache.Item {
    Key:   "lyric",
    Value: []byte("Oh, give me a home"),
}

这篇关于预期的声明,找到"IDENT"项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆