Can Golang modules be used on a branch other than master?

3.4k Views Asked by At

This code works as long as I'm on the master branch:

main.go:

package main

import (
    datemodlocal "192.168.0.12/gitrepo/go-module-test-dateutil.git"
    stringmodlocal "192.168.0.12/gitrepo/go-module-test-stringutil.git"
    "fmt"
    "github.com/dwschulze/go-module-test-dateutilmod"
    "github.com/dwschulze/go-module-test-stringutilmod"
)

func main() {

    fmt.Println("github:  " + stringmod.ToUpperCase("test"))
    fmt.Println("github:  " + datemod.GetTime().String())
    fmt.Println("local:  " + stringmodlocal.ToUpperCase("test"))
    fmt.Println("local:  " + datemodlocal.GetTime().String())
}

go.mod:

module module-driver

require (
    192.168.0.12/gitrepo/go-module-test-dateutil.git v0.0.1
    192.168.0.12/gitrepo/go-module-test-stringutil.git v0.0.1
    github.com/dwschulze/go-module-test-dateutilmod v0.0.1
    github.com/dwschulze/go-module-test-stringutilmod v0.0.1
)

go 1.15

I need to use the branch dev2 for development. The godocs don't show what needs to be done to the import path or the require statement in go.mod. If I change the import statement to:

datemodlocal "192.168.0.12/gitrepo/go-module-test-dateutil.git@dev2"

I get:

$ go run main.go
package command-line-arguments
imports 192.168.0.12/gitrepo/go-module-test-dateutil.git@dev2: can only use path@version syntax with go get

If I move the @dev2 to the require statement in go.mod

192.168.0.12/gitrepo/go-module-test-dateutil.git@dev2 v0.0.1

I get

$ go run main.go
go: 192.168.0.12/gitrepo/go-module-test-dateutil.git@[email protected]: unrecognized import path "192.168.0.12/gitrepo/go-module-test-dateutil.git@dev2": https fetch: Get "https://192.168.0.12/gitrepo/go-module-test-dateutil.git@dev2?go-get=1": dial tcp 192.168.0.12:443: connect: connection refused

That error message says https which is strange since in my ~/.gitconfig I have

[url "[email protected]:"] insteadOf = https://192.168.0.12/

Setting GOPRIVATE has no effect. If I put the @dev2 in both places I get the same error messages.

The godocs don't show any examples of what a working .go and go.mod file would have to contain to use modules on a branch other than master. I would think that modules would have to work on any branch because development is often done on branches other than master.

Does anyone have a working example of a .go file and a go.mod that work on a branch other than master?

2

There are 2 best solutions below

3
On

Go's tooling has no notion of a SNAPSHOT or development or moving dependency. Dependencies are fixed and trying to circumvent this by go geting a branch will lead to more problems.

If you have a moving dependency:

  1. Add a replace directive to go.mod making this dependency point to a local copy of it.
  2. Manage that local copy of the dependency the way you want, e.g. by checking out a certain branch and git pulling regularly.

If you have an Go-unusable git server:

If you have a VCS server which doesn't provide meta tags as described in https://golang.org/pkg/cmd/go/#hdr-Remote_import_paths you must use a replace directive and manage the local copy manually with your VCS.

In any case: An import path of the form "192.168.0.12/gitrepo/go-module-test-stringutil.git" (with a .git suffix) is wrong as explained in https://golang.org/pkg/cmd/go/#hdr-Remote_import_paths: The ".git" should go to "192.168.0.12/gitrepo.git/go-module-test-stringutil" if "gitrepo" actually is the repo.

0
On

As far as I found, It is not possible to use branch name in Golang Modules(go.mod).
Instead, use git hash.

go get -u github.com/your/repo@{git-hash}