Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

expected declaration, found 'IDENT' item

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"), } 

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

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

like image 573
Karthic Rao Avatar asked Mar 09 '15 12:03

Karthic Rao


1 Answers

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

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 } 

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"), } 
like image 51
icza Avatar answered Sep 21 '22 16:09

icza