简体   繁体   中英

go protobuf: Cannot find package "." in github.com/gogo/protobuf/proto and m.TimeStamp.MarshalToSizedBuffer undefined

Question as per title. Tried to compile in 2 ways gogoproto and golangprotobuf.

Wrote tests for both, and both won't marshal.

msg.proto

syntax = "proto3";

import "google/protobuf/timestamp.proto";

package msg;

message Message {
    string Name = 1;
    google.protobuf.Timestamp TimeStamp = 2;
}

demo_test.go

package msg

import (
    "testing"
    "time"

    // gogo "github.com/gogo/protobuf/proto"
    "github.com/golang/protobuf/proto"
    "github.com/golang/protobuf/ptypes/timestamp"
)

var msg = Message{
    Name:      "demo",
    TimeStamp: &timestamp.Timestamp{Seconds: int64(time.Now().Second())},
}

//func TestGogoMessage_Marshal(t *testing.T) {
//  myBytes, err := gogo.Marshal(&msg)
//  if err != nil {
//      t.Fail()
//  }

//  _ = myBytes
//}

func TestProtoMessage_Marshal(t *testing.T) {
    myBytes, err := proto.Marshal(&msg)
    if err != nil {
        t.Fail()
    }

    _ = myBytes
}

compiled with:

protoc --gofast_out=. ./demo/msg.proto protoc --gofast_out=. ./demo/msg.proto works, but running the test:

# github.com/.../demo
package github.com/.../demo (test)
    imports github.com/gogo/protobuf/proto: cannot find package "." in:
    /Users/.../vendor/github.com/gogo/protobuf/proto

protoc --go_out=. ./demo/msg.proto protoc --go_out=. ./demo/msg.proto works, but running the test:

# github.com/.../demo [github.com/.../demo.test]
./msg.pb.go:127:28: m.TimeStamp.MarshalToSizedBuffer undefined (type *timestamp.Timestamp has no field or method MarshalToSizedBuffer)
./msg.pb.go:169:18: m.TimeStamp.Size undefined (type *timestamp.Timestamp has no field or method Size)
./msg.pb.go:277:25: m.TimeStamp.Unmarshal undefined (type *timestamp.Timestamp has no field or method Unmarshal)

Both commands work fine for me, so, probably problem is in in your environment.

Regarding undefine error, it looks like you're using Timestamp struct from github.com/golang/protobuf/ptypes/timestamp , which has different interface than Timestamp from https://github.com/gogo/protobuf/blob/master/types/timestamp.pb.go . So if you generate msg.pb.go with protoc --gofast_out=. ./demo/msg.proto protoc --gofast_out=. ./demo/msg.proto you will be getting this error.

add more options:

protoc -I xxx --gogofaster_out=plugins=grpc,paths=source_relative,Mgoogle/protobuf/any.proto=github.com/gogo/protobuf/types,Mgoogle/protobuf/duration.proto=github.com/gogo/protobuf/types,Mgoogle/protobuf/timestamp.proto=github.com/gogo/protobuf/types:${PB_DIR} xxx.proto

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM