S
S
stripwire2021-01-18 09:29:59
go
stripwire, 2021-01-18 09:29:59

How to import internal dependencies in golang?

Let's say my project is in ~/project/ .
I want to connect ~/project/pkg/foo to ~/project/main/main.go.

Can I do it under the following conditions?
- don't move the project to $GOPATH/src
- don't use import "github.com/user/project/..." - do
n't import "../pkg/foo" (may cause errors)

in the future, this scenario did not occur:
- $ git clone project
- $ go build ...
- Go starts pumping the same turnip into $GOPATH/src/github.com

(I'm new to go, and I just learned about go modules. Therefore examples are likely to be crooked)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel Gryaznov, 2021-01-19
@GRbit

The orthodox correct way to specify a dependency is via "github.com/user/project/..."
You can just go look at the code for large projects like
https://github.com/hashicorp/consul/blob/master/main.go
https:/ /github.com/docker/engine/blob/master/cmd/d...
If there is an urgent need to do all this right at home and never upload it anywhere, you can do this:
go mod init localpack
yellow.go:

package localpack

func Hi(s string) string {
   return "Hi " +s + ". I'm local package."
}

orange/orange.go:
package main
import "localpack"

func main() {
   s := localpack.Hi("Red")
   println(s)
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question