【问题标题】:Can't use struct from own package不能使用自己包中的结构
【发布时间】:2014-05-16 19:14:10
【问题描述】:

我在 $GOPATH/src 中创建了以下文件结构

bitbucket.org/MyName/ProjectName

我这里有以下文件

ProjectName
 - controllers/
    - meController.go
 - app.go

在 app.go 中,我正在像这样导入我的控制器:

import "bitbucket.org/MyName/ProjectName/controllers"

在 main func 中我正在尝试使用它的方法。

meController = new(controllers.meController)
m.Get("/", meController.Index)

我的 meController.go 看起来像这样

package controllers

type meController struct {

}

func (controller *meController) Index () string {
  return "Hello World"
}

但是我收到了这个错误:

./app.go:5: imported and not used: "bitbucket.org/MyName/ProjectName/controllers"
./app.go:12: undefined: meController

我不知道如何让它工作。

有什么想法吗?

谢谢!

【问题讨论】:

    标签: go martini


    【解决方案1】:

    在 Go 中,每个以小写字母开头的符号都不会被包导出。打电话给你的结构MeController,你会没事的。

    【讨论】:

    • 不知道!非常感谢。
    • 它是 Go 中最重要的习语之一,结构成员也是如此——这是一种简单的表示公共/私有的方式。有些人喜欢它,有些人讨厌它。我个人喜欢它。
    猜你喜欢
    • 1970-01-01
    • 2023-03-07
    • 2011-09-13
    • 1970-01-01
    • 2013-07-04
    • 2019-12-30
    • 1970-01-01
    • 1970-01-01
    • 2012-05-18
    相关资源
    最近更新 更多