How to import local packages without gopath

后端 未结 8 2002
有刺的猬
有刺的猬 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:33

    I have a similar problem and the solution I am currently using uses Go 1.11 modules. I have the following structure

    - projects
      - go.mod
      - go.sum
      - project1
        - main.go
      - project2
        - main.go
      - package1
        - lib.go
      - package2
        - lib.go
    

    And I am able to import package1 and package2 from project1 and project2 by using

    import (
        "projects/package1"
        "projects/package2"
    )
    

    After running go mod init projects. I can use go build from project1 and project2 directories or I can do go build -o project1/exe project1/*.go from the projects directory.

    The downside of this method is that all your projects end up sharing the same dependency list in go.mod. I am still looking for a solution to this problem, but it looks like it might be fundamental.

提交回复
热议问题