Go for protobuf中的相对导入,找不到模块路径

时间:2019-12-17 10:37:43

标签: go protocol-buffers go-modules grpc-go

我正在尝试与gRPC一起编写服务,并且当我导入protobuff文件时出现错误。我尝试删除go路径中的所有模块,然后重新初始化go模块

build _/Users/tibinlukose/cart-service/pb: cannot find module for path _/Users/tibinlukose/cart-service/pb

代码

package main

import (
    pbcart "../pb/"
    "log"
    "fmt"
    "google.golang.org/grpc"
    "net"
)

var (
    port = 1000;
)

type CartServiceServer struct {
}

func main() {
    log.SetFlags(log.LstdFlags | log.Lshortfile)
    fmt.Println("Server Starting ..")
    lis, err := net.Listen("tcp", fmt.Sprintf("localhost:%d", 10000))
    if err != nil {
        log.Fatal("unable to listen on the port")
    }
    serverOptions := []grpc.ServerOption{}
    grpcServer := grpc.NewServer(serverOptions...)
    srv := &CartServiceServer{}
    pbcart.RegisterCartServiceServer(grpcServer, srv)
}

env

GOCACHE="/Users/tibinlukose/Library/Caches/go-build"
GOENV="/Users/tibinlukose/Library/Application Support/go/env"
GOPATH="/Users/tibinlukose/go"
GOROOT="/usr/local/Cellar/go/1.13.4/libexec"
GOTOOLDIR="/usr/local/Cellar/go/1.13.4/libexec/pkg/tool/darwin_amd64"
GOMOD="/Users/tibinlukose/cart-service/server/go.mod"

回购https://github.com/zycon/cart-service

1 个答案:

答案 0 :(得分:2)

go.mod移至根目录并更新导入到github.com/zycon/cart-service/pb吗?

Go中没有相对导入。您可以看到以下答案以获取详细说明:Relative imports in Go

有一个建议:https://github.com/golang/go/issues/20883