从其他包导入的结构未定义 [英] Imported struct from other package is undefined

查看:49
本文介绍了从其他包导入的结构未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我讨厌的问题.我的models/model.go

Here is my noobish problem. I have this struct in my models/model.go

    package models

    import (
      "time"
      "gopkg.in/mgo.v2/bson"

    )

    type Horse struct {
        Id    bson.ObjectId `bson:"_id,omitempty"`
        Title       string
        Description string
        CreatedOn   time.Time
        Creator     string
        Visits  int
        Score       int

    }

在我的controllers/crud.go中,我正在尝试使用Horse结构

And in my controllers/crud.go I'm trying to use Horse struct

package controllers

import (
  "html/template"
  "log"
  "net/http"
  "horseapp/models"
)
[...]
var horseStore = make(map[string]Horse) //This raises undefined error

但是当我go install horseapp时我得到了undefined: Horse.

这是怎么了?如何解决?

What is wrong here and how to fix it?

推荐答案

使用

var horseStore = make(map[string]models.Horse)

从另一个包访问标识符时,必须始终在包名前加上一个包名和一个点:package.Identifier

When accessing an identifier from another package you will always have to prefix it with the packages name and a dot: package.Identifier

这篇关于从其他包导入的结构未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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