A
A
Ao2021-03-04 12:22:34
go
Ao, 2021-03-04 12:22:34

How to use Go modules correctly?

I do not understand how to use modules without publishing them in the version control system?
For example, I create a folder

c: / Project
in it a folder for modules
c : / Project / Modules.pack, then a
folder for the c: / Project / Modules.pack / mod1
submodule,

I do go mod init Modules.pack / mod1 in it,
then go get
everything is ok it seems to tighten up the dependencies and add from go.mod

now I want to make a module that will use mod1

c:/Project/Modules.pack/mod2
in the mod2.go file

package mod2
import (
  . "Modules.pack/mod1"	
)
....


I do in go mod init Modules.pack/mod2
go get
and I get the error
Modules.pack/mod2 imports
Modules.pack/mod1: cannot find module providing package Modules.pack/mod1

I don’t understand how and where to place and how to specify paths in import for my modules

I don't understand how to use these modules in the program? how to import them

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Mirilaczvili, 2021-03-05
@2ord

If you need an example of how to use it locally, you can see an example in the go-openvz-api repository . This is the first code commit (commit), before using go modules.
Later in the commit history, I subsequently switched to go modules.
main.go

package main
import "./package1"
...

package1/file1.go
package package1
import (
  "fmt"
)
...

package1/file2.go
package package1
...

If you initially apply the package naming scheme as for storage on remote repositories, then you can do this:
go mod init example.org/user/mypackage
This means that the import prefix in packages will always begin with example.org/user/mypackage. Everything in the subdirectories of the project, when importing, must be specified as example.org/user/mypackage/название_подпапки(for example, example.org/user/mypackage/package1).
And important additions, without which nothing will work:
1. The project must be located outside the GOPATH path.
2. Edit go.mod by adding the line
replace example.org/user/mypackage => ./
This instruction will point to a local folder located on the same path as the project root. This way go won't look up hosting http://example.org/user/mypackage
This will work for Linux where paths are specified with a slash (the leading dot is the current directory). On Windows, the backslash is used. I don't know how Go works with paths on Windows.
That is, you can work locally and not upload the code anywhere, everything will work locally.
And when you nevertheless upload the code to the repository, change everywhere example.org/user/mypackageto the true location of the repository, and delete the line replacefrom go.mod, otherwise it will only look for the code locally.
Notes:
1. It is possible that the name Modules.pack is problematic because of the dot inside.
2. so wrong. You can remove the dot first . You can do this: or this: or this:. "Modules.pack/mod1"
"./Modules.pack/mod1"
"example.org/user/Modules.pack/mod1"
"qqw example.org/user/Modules.pack/mod1" // а в самом коде ниже писать qqw вместо mod1

Instead I go getuse go mod tidy.
I myself have been working with Go modules not so long ago, but I think that this is how it should be.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question