2

I am trying to use the module capabilities of go to understand how they work. I have read the documentation and even replicated the exact process and for some reason the import of local modules. The file tree is like this

    src
     |
      main -> main.go
     |
     pkg -> pkg.go 

The src folder has two folders main and pkg.

My question is where should I call go mod init and how should I name it. This has been confusing me for a while now.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Supreet Singh
  • 87
  • 1
  • 8
  • 3
    The official docs have a [step by step tutorial](https://golang.org/doc/tutorial/create-module), and the [linked reference](https://golang.org/ref/mod#go-mod-init) explains `go mod init` in detail. You run `go mod init` where you want to initialize your module, and you name it what you want to name your module. What specific questions do you have? – JimB May 19 '21 at 15:20
  • 2
    A "module" is a set of packages versioned together. You put your go.mod somewhere and put all packages with the same lifecycle in a folder below. Anything else doesn't matter. Just read the official tuttorial. – Volker May 19 '21 at 15:22
  • 3
    The introductory tutorial [How to Write Go Code](https://golang.org/doc/code) covers your scenario. The tutorial shows how [write a program in a module](https://golang.org/doc/code#Command) that [imports a package from the same module](https://golang.org/doc/code#ImportingLocal). – Charlie Tumahai May 19 '21 at 15:41
  • 1
    Thanks to everyone for the answers. I spent the whole day going through the documentation and going through this book called "head first go", but couldn't import the modules I made. – Supreet Singh May 19 '21 at 16:43
  • Although it doesn't explain the name chosen for use with `go mod init`, this is probably the best place to start: [Tutorial: Get started with Go](https://go.dev/doc/tutorial/getting-started). A key point is that "Enable dependency tracking for your code" is optional for the most trivial cases like `go run hello.go`, but is essentially required for any non-trivial applications or workflows (for example using non-standard imports). – Brent Bradburn Nov 14 '22 at 02:34
  • Another point that is rarely mentioned is that it basically doesn't matter what you use as the module name if you're building a main module that you don't plan to import elsewhere. – Brent Bradburn Nov 14 '22 at 02:34

1 Answers1

2

I will use Windows paths, as that's what I use. First, make a new folder somewhere, for example C:\north. They go to that directory, and enter this:

go mod init north

Then make C:\north\north.go:

package north

const Direction = "north"

Then make C:\north\north\north.go:

package main
import "north"

func main() {
   println(north.Direction)
}
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Zombo
  • 1
  • 62
  • 391
  • 407
  • 4
    A module name that is not a URI is fine for testing and local development, but it's also one more thing to trip up new users which can easily be avoided. – JimB May 19 '21 at 16:09
  • This does solve my problem but I will not be able to explain how this work, I will study from the notes that you people linked. Thanks for your time. – Supreet Singh May 19 '21 at 16:42