简体   繁体   中英

How to make Go search for imports beyond GOROOT

I'm migrating dep project into go modules, and some Go files in this project import proto files, but when I execute go mod tidy , it says that cannot find the package where proto files are. Project's structure:

version_service
   proto
     authentication
       authentication.proto
     version
       version.proto
 repository.go

I'm importing like this in repository.go

pb "proto/version"

And i have this:

proto/version: package proto/version is not in GOROOT 

How import this files?

GOROOT is meant to "store" Go native packages, so you shouldn't use it for your own packages, instead, you need to use Go Modules .

You can interpret Go Modules almost like a native package manager for your Go projects, for example, in your project with the following structure:

version_service
  proto
   authentication
     authentication.proto
   version
     version.proto
  repository.go

In you shell you can start the Go Modules with go mod init package.name , in your case package.name could be proto , so using your project root as reference you could import packages using proto/version

package authentication

import (
   pb "proto/version"
)

func main() {
    pb.Foo().Bar()

}

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