包导入,不能使用导入包中的struct [英] Package imports, cannot use struct from imported package

查看:48
本文介绍了包导入,不能使用导入包中的struct的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是菜鸟的错误,但我无法弄清楚.

This is probably a noob mistake but I cannot figure this out.

在main.go中,我正在导入软件包.

In main.go I am importing a package.

import(
    "models/users"
)

// ...

func main() {
    r.HandleFunc("/users/list", UsersModel.List())

该软件包存储在src/models/users中

The package is stored in src/models/users

用户包如下所示:

package users

import (
    "gopkg.in/mgo.v2"
)

// ...

/**
 * User Model
 */
type UsersModel struct {

}

// Add methods to the UsersModel type.
func (m *UsersModel) List() {
// ...

运行代码时,出现以下错误.

When I run the code I get the following error.

src \ main.go:9:已导入且未使用:模型/用户" src \ main.go:20:未定义:UsersModel

src\main.go:9: imported and not used: "models/users" src\main.go:20: undefined: UsersModel

go代码似乎是有效的,好像我将其抛出错误一样.如何从用户包中导出UsersModel类型?

The go code appears to be valid as if I invalidate it throws an error. How do I export the UsersModel type from the users package?

推荐答案

您需要对 UsersModel 的使用加上包名称 users 的前缀,例如:

You need to prefix your use of UsersModel with the package name users, like so:

um := users.UserModel{}


UsersModel.List()也是错误的:go没有静态方法"或类方法".


UsersModel.List() is also wrong: go doesn't have "static methods" or "class methods".

这篇关于包导入,不能使用导入包中的struct的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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