Relative import in Go for protobuf , cannot find module path

扶醉桌前 提交于 2020-07-31 06:05:36

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!