英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论