问题
I'm trying to write a service in go with gRPC, and when i import the protobuff file , getting an error. i tried removing all the modules in my go path and reinitialising the go modules
build _/Users/tibinlukose/cart-service/pb: cannot find module for path _/Users/tibinlukose/cart-service/pb
Code
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"
repo https://github.com/zycon/cart-service
回答1:
Move your go.mod
to the root and update import to github.com/zycon/cart-service/pb
?
There is no relative import in Go. You can see this answer for an extended explanation: Relative imports in Go
There is a proposal: https://github.com/golang/go/issues/20883
来源:https://stackoverflow.com/questions/59372407/relative-import-in-go-for-protobuf-cannot-find-module-path