douaonong7807 2018-04-01 06:49
浏览 302

使用来自不同包golang的struct

I'm calling to object from current file which works (i'm creating unit test)

type Requires struct {
    Name       string     `yaml:"name,omitempty"`
}

and I call it inside test like

Requires: []Requires{
{
    Name: "db",
}

which works ok,

Now I move the Requires struct to different package models

and I try to call it like

Requires: models.Requires{
{
    Name: "db",
}

cannot use models.require as type []Require

also tried with models.[]Requires

This give me the error

use of package without selector error

Getting a use of package without selector error

But not sure how to handle it for my case...

Any idea how to overcome this ?

UPDATE

When I try it like following

        Requires: models.Requires{
                Name: “db",

                },
            },

I got error

cannot use models.Requires literal (type models.Requires) as type []models.Requires in field value

This is the error from viscose

  • 写回答

1条回答 默认 最新

  • dqf98772 2018-04-01 07:47
    关注

    The package is models which contains Requires struct used as slice. Have a look at Qualified identifiers for more understanding of how package works. In your case It needs a slice of Requires with models package, should be used as below:

    Requires: []models.Requires{
    {
        Name: "db",
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大