Whats a good best practice with Go workspaces?

后端 未结 10 1379
南方客
南方客 2021-01-30 10:46

I\'m just getting into learning Go, and reading through existing code to learn \"how others are doing it\". In doing so, the use of a go \"workspace\", especially as it relates

相关标签:
10条回答
  • 2021-01-30 11:19

    I follow KISS - one GOPATH, two go paths:

    export GOPATH=$HOME/go:$HOME/development/go

    That way third party stuff goes in a central place (package install uses the first path entry by default), and I can flexibly move my projects elsewhere, at the second path entry.

    0 讨论(0)
  • 2021-01-30 11:20

    Using one GOPATH across all of your projects is very handy, but I find this to only be the case for my own personal projects.

    I use a separate GOPATH for each production system I maintain because I use git submodules in each GOPATH's directory tree in order to freeze dependencies.

    So, something like:

    ~/code/my-project
    - src
      - github.com
        + dependency-one
        + dependency-two
        - my-org
          - my-project
            * main.go
            + package-one
            + package-two
    - pkg
    - bin
    

    By setting GOPATH to ~/code/my-project, then it uses the dependency-one and dependency-two git submodules within that project instead of using global dependencies.

    0 讨论(0)
  • 2021-01-30 11:21

    I think it's easier to have one $GOPATH per project, that way you can have different versions of the same package for different projects, and update the packages as needed.

    With a central repository, it's difficult to update a package as you might break an unrelated project when doing so (if the package update has breaking changes or new bugs).

    0 讨论(0)
  • 2021-01-30 11:23

    Just use GoSwitch. Saves a heck of a lot of time and sanity. Add the script to the root of each of your projects and source it. It will make that project dir your gopath and also add/removes the exact bin folder of that project to path. https://github.com/buffonomics/goswitch

    0 讨论(0)
提交回复
热议问题