dongyinshua9996 2017-03-20 23:11
浏览 143
已采纳

ProtoBuf的Golang解析

I'm new to Golang and I am trying to write an home automation framework in Golang, using the Micro framework and Protobuf framework.

I am currently having a hard time trying to implement a simple registry type service.

An example of the problem I am having is that I have the following, I want to be able to get a list of devices, provided a client does a GET request to http://localhost:8080/view/devices

I have the following protobuf definition:

syntax = "proto3";

service DRegistry {
    rpc View(ViewRequest) returns (DeviceRegistry) {}
} 

message DeviceRegistry {
    repeated Device devices = 1;
}

message ViewRequest {
    string Alias = 1;
}

message Device {
    string Alias = 1;
    string HWAddress = 2;
    string WakeUpMethod = 3;
    repeated string BoundServices = 4;
}

Then in my service defination I have the following:

package main

import (
    "log"

    micro "github.com/micro/go-micro"
    proto "github.com/srizzling/gotham/proto/device"

    "golang.org/x/net/context"
)

// DRegistry stands for Device Registry and is how devices register to Gotham.
type DRegistry struct{}

var devices map[string]proto.Device

func (g *DRegistry) View(ctx context.Context, req *proto.ViewRequest, rsp *proto.DeviceRegistry) error {
    filter := req.Alias
devices, err := filterDevices(filter)
rsp.Devices = devices
}

func filterDevices(filter string) (*[]proto.Device, error) {
    // Currently only supports listing a single service for now
    // TODO: expand filter to be more consise
    filteredDevices := make([]proto.Device, 0, len(devices))
    for _, e := range devices {
        for _, f := range e.BoundServices {
            if f == filter {
                filteredDevices = append(filteredDevices, e)
            }
        }
    }
    return &filteredDevices, nil
}

func main() {
    service := micro.NewService(
        micro.Name("DRegistry"),
    )
    proto.RegisterDRegistryHandler(service.Server(), new(DRegistry))

    if err := service.Run(); err != nil {
        log.Fatal(err)
    }
}

The problem I am having is that my IDE (Visual Studio Code) is complianing that I cannot use devices (type *[]device.Device) as type []*device.Device in assignment which is confusing.

TLDR: How do I assign a collection of proto.Devices to the proto.DeviceRegistry?

  • 写回答

1条回答 默认 最新

  • douyiyi5284 2017-03-21 00:19
    关注
    func filterDevices(filter string) ([]*proto.Device, error) {
        // Currently only supports listing a single service for now
        // TODO: expand filter to be more consise
        filteredDevices := make([]*proto.Device, 0, len(devices))
        for _, e := range devices {
            for _, f := range e.BoundServices {
                if f == filter {
                    filteredDevices = append(filteredDevices, &e)
                }
            }
        }
        return filteredDevices, nil
    }
    

    There is a difference between a slice of pointers ([]*) and a pointer to a slice (*[]). You are returning a pointer to slice, whereas what you want is a slice of pointers. We can solve this by:

    • Updating your filterDevices signature to return a slice of pointers
    • Updating your make call to make a slice of pointers
    • Taking the address of e in your call to append (we want a slice of pointers to devices)
    • Not returning the address of the slice
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀