Home > other >  Go module where package name is different to github path
Go module where package name is different to github path

Time:10-24

I want to add a go package to my go.mod file. But it's called e.g. https://github.com/user/something but the actual package in the source files is e.g. somethingelse. How can I add this to my go.mod file? Just doing this doesn't work:

import somethingelse github.com/user/something

I get an error:

module declares its path as: somethingelse
        but was required as: github.com/user/something

CodePudding user response:

If you want to add a Go package to your go mod file first download your package to your local repo using

go get github.com/user/something

Then import the package to your program it will get imported, well if you are using Visual studio code then your package will be auto imported when you write code with the syntax of that specific package and save the program.

If that doesn't import automatically, then import it manually or just run a command on your IDE terminal which is

go mod tidy

This command will basically match the go.mod file with the dependencies required in the source files.

  • Download all the dependencies that are required in your source files and update go.mod file with that dependency.
  • Remove all dependencies from the go.mod file which are not required in the source files.

CodePudding user response:

Try adding the following to your go.mod file:

replace somethingelse => github.com/user/something latest

then, import as:

import "somethingelse"
  •  Tags:  
  • go
  • Related