CookieOfFortune
CookieOfFortune

Reputation: 14004

How do I declare a variable with a type from another package in Go?

For example:

package foo

import (
    "appengine/blobstore"
)

func main() {
    var blob blobstore.BlobInfo
    ...
}

Gives me this error: undefined: BlobInfo

Is there a way for me to be able to create this struct without having to copy the code over?

Upvotes: 2

Views: 935

Answers (1)

zzzz
zzzz

Reputation: 91369

If blobstore.BlobInfo is a type then declare a variable of that type in another package like:

var blob blobstore.BlobInfo

The syntax

var foo = bar.Qux

attempts to create var foo and initialize it by assigning it the value of bar.Qux while inferring bar.Qux's type.

EDIT:

To declare a variable of type T

var v T

T can come from other package. For example

import "foo/bar"
import baz "qux"
import . "whatever"

var v1 bar.T
var v2 baz.T
var v3 T // whatever.T

If this doesn't work for you then some of the possible problems are:

  • Package blobstore is not instaled.
  • Package blobstore is not found in your GOPATH using the import path shown in the OP.

Upvotes: 3

Related Questions