How to import local packages without gopath

后端 未结 8 1955
有刺的猬
有刺的猬 2020-12-07 07:58

I\'ve used GOPATH but for this current issue I\'m facing it does not help. I want to be able to create packages that are specific to a project:

         


        
8条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-07 08:18

    To add a "local" package to your project, add a folder (for example "package_name"). And put your implementation files in that folder.

    src/github.com/GithubUser/myproject/
     ├── main.go
     └───package_name
           └── whatever_name1.go
           └── whatever_name2.go
    

    In your package main do this:

    import "github.com/GithubUser/myproject/package_name"
    

    Where package_name is the folder name and it must match the package name used in files whatever_name1.go and whatever_name2.go. In other words all files with a sub-directory should be of the same package.

    You can further nest more subdirectories as long as you specify the whole path to the parent folder in the import.

提交回复
热议问题