登录
首页 >  Golang >  Go问答

指针无法使用是由原型生成的结构导致的

来源:stackoverflow

时间:2024-03-27 16:00:30 316浏览 收藏

对于一个Golang开发者来说,牢固扎实的基础是十分重要的,golang学习网就来带大家一点点的掌握基础知识点。今天本篇文章带大家了解《指针无法使用是由原型生成的结构导致的》,主要介绍了,希望对大家的知识积累有所帮助,快点收藏起来吧,否则需要时就找不到了!

问题内容

我使用 protobuf 创建了结构,现在我无法使用嵌套结构,因为它是一个指针。问题是我无法更改 proto 文件。我必须使用生成的内容。有人可以解决我的问题吗?我想这应该很简单。

这是生成的结构:

type dashboard struct {
    id                   string   `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
    name                 string   `protobuf:"bytes,2,opt,name=name,proto3" json:"name,omitempty"`
    description          string   `protobuf:"bytes,3,opt,name=description,proto3" json:"description,omitempty"`
    hidden               bool     `protobuf:"varint,4,opt,name=hidden,proto3" json:"hidden,omitempty"`
    layout               *layout  `protobuf:"bytes,5,opt,name=layout,proto3" json:"layout,omitempty"`
    iscanned             bool     `protobuf:"varint,6,opt,name=is_canned,json=iscanned,proto3" json:"is_canned,omitempty"`
    userid               string   `protobuf:"bytes,7,opt,name=user_id,json=userid,proto3" json:"user_id,omitempty"`
    shared               bool     `protobuf:"varint,8,opt,name=shared,proto3" json:"shared,omitempty"`
    displaytitle         bool     `protobuf:"varint,9,opt,name=display_title,json=displaytitle,proto3" json:"display_title,omitempty"`
    tenantid             string   `protobuf:"bytes,10,opt,name=tenant_id,json=tenantid,proto3" json:"tenant_id,omitempty"`
    xxx_nounkeyedliteral struct{} `json:"-"`
    xxx_unrecognized     []byte   `json:"-"`
    xxx_sizecache        int32    `json:"-"`
}

type layout struct {
    griditems            []*griditem `protobuf:"bytes,1,rep,name=grid_items,json=griditems,proto3" json:"grid_items,omitempty"`
    xxx_nounkeyedliteral struct{}    `json:"-"`
    xxx_unrecognized     []byte      `json:"-"`
    xxx_sizecache        int32       `json:"-"`
}

type griditem struct {
    id                   string   `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
    viewid               string   `protobuf:"bytes,2,opt,name=view_id,json=viewid,proto3" json:"view_id,omitempty"`
    cols                 int32    `protobuf:"varint,3,opt,name=cols,proto3" json:"cols,omitempty"`
    rows                 int32    `protobuf:"varint,4,opt,name=rows,proto3" json:"rows,omitempty"`
    x                    int32    `protobuf:"varint,5,opt,name=x,proto3" json:"x,omitempty"`
    y                    int32    `protobuf:"varint,6,opt,name=y,proto3" json:"y,omitempty"`
    xxx_nounkeyedliteral struct{} `json:"-"`
    xxx_unrecognized     []byte   `json:"-"`
    xxx_sizecache        int32    `json:"-"`
}

当我尝试创建新的仪表板时,我无法为其分配布局,因为它是一个指针。

dashboard := &types.Dashboard{
    Id:           uuid.Must(uuid.NewV4()).String(),
    Name:         request.Name,
    Description:  request.Description,
    Hidden:       request.Hidden,
    Layout:       request.Layout,
    IsCanned:     request.IsCanned,
    Shared:       request.Shared,
    DisplayTitle: request.DisplayTitle,
    UserId:       tokenValues.UserID,
    TenantId:     tokenValues.TenantID,
}

正确答案


您可以使用如下所示的结构引用。

func main() {
    grids := []*GridItem{
        &GridItem{
            Id: "grid_1",
        },
    }
    layout := &Layout{
        GridItems: grids,
    }
    dashboard := Dashboard{
        Layout: layout,
    }
    fmt.Println(dashboard.Layout.GridItems[0].Id)
}
//Output: grid_1

您可以运行here

好了,本文到此结束,带大家了解了《指针无法使用是由原型生成的结构导致的》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>