无法使用自己包中的结构体。

huangapple go评论82阅读模式
英文:

Can't use struct from own package

问题

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

bitbucket.org/MyName/ProjectName

这里有以下文件:

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

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

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

在main函数中,我尝试使用它的方法:

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

我不知道如何让它工作。

有什么建议吗?

谢谢!

英文:

I created following file structure in $GOPATH/src

bitbucket.org/MyName/ProjectName

I have follwoing files here

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

In app.go I'm importing my controller like that:

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

And in main func I'm trying to use it's method.

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

My meController.go looks like this

package controllers

type meController struct {

}

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

But I'm getting this error:

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

I don't have any idea how to get this to work.

Any ideas?

Thanks!

答案1

得分: 5

在Go语言中,以小写字母开头的符号不会被包导出。将你的结构体命名为MeController,问题就会解决。

英文:

In Go, every symbol that starts with lowercase is not exported by the package. call your struct MeController and you'll be fine.

huangapple
  • 本文由 发表于 2014年5月17日 03:14:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/23702986.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定