How to set the version number?

In https://golangbyexample.com/packages-modules-go-second/ the author talks about Importing package from different module locally. Basically what is said here is:

Make directory tree like

.
├── math
│   ├── go.mod
│   └── math.go
└── school
    ├── go.mod
    └── school.go

In math directory run go mod init sample.com/math

In school directory run go mod init school

math.go file should look like

package math

func Add(a, b int) int {

return a + b

}

school.go file should look like

package main

import (

"fmt"

"sample.com/math"

)

func main() {

fmt.Println(math.Add(2, 4))

}

school/go.mod file should look like

module school

go 1.13

replace sample.com/math => ../math

In school directory run go run school.go

Now you will see that school/go.mod is changed to

module school

go 1.13

replace sample.com/math => ../math

require sample.com/math v0.0.0-00010101000000-000000000000 // indirect

The thing that is bothering me is, neither school nor math is under any sort of version control (both are in my local machine). So, where is it getting this version number.

  1. How to set the version number that is showed in go.mod file (in this case v0.0.0-00010101000000-000000000000)?
  2. How to import a different version of a package from different module locally?

Hi @blueray453,

Sorry for the late answer but I came across your question just now. I hope you already resolved it for yourself, but in case you didn’t, and in the interest of other readers, let me drop a few words here.

The answer to question 1 is quite simple - if a package has no version, the Go command assigns a pseudo version to that package.

If the sample.com/math package is under your control, you can add a version by tagging the repository with a proper SemVer version string. Otherwise you would have to ask the package owner to tag their package accordingly.

Not sure if I understand question 2 correctly, but when you use the replace directive, then you point the Go compiler to code that is right on your disk. To choose a different version of that code, you would need to check out the code at the desired version tag.

1 Like