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
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?