Go 'mod init' creating new folders? what is the significance of path?
Asked Answered
S

1

9

Just 3 days experience in Go language. Hope an example will be more apt to understand my confusion.

root@freebsd1:/usr/home/arun/go-start/src/test2 # go mod init f1/f2/f3/f4/f5/hello
go: creating new go.mod: module f1/f2/f3/f4/f5/hello
root@freebsd1:/usr/home/arun/go-start/src/test2 #

Here in above example go mod init is creating all these folders(f1/f2/f3/f4/f5/hello)?. I searched a lot, couldn't find any such folders in system. Then what is the significance of this path.

Even though below command will not run if this path is not mentioned as it is

# go install f1/f2/f3/f4/f5/hello

--:EDIT:--

May be this will help someone later ( Just walk through the steps to understand this in a proper way, especially for newbies )

  1. I am planning to create a program 'calculator' and will upload in GitHub later.

  2. I will keep the functions in different packages like sum,multiply etc

  3. first step #go mod init github.com/go-arun/calculator( Don't confuse here , this is just an assumption that, in future I may create a repository in github for this project )

  4. created folder sum(one of the package folder , and created sum.go inside )

See those in by system:

1.

root@debian1:/home/arun/lab# go mod init github.com/go-arun/calculator
go: creating new go.mod: module github.com/go-arun/calculator

root@debian1:/home/arun/lab# cat go.mod
module github.com/go-arun/calculator

go 1.15

2.

root@debian1:/home/arun/lab# cat sum/sum.go
package sum

import "fmt"

func Sum(num1,num2 int)(){
        fmt.Println(num1+num2)

}

3.

root@debian1:/home/arun/lab# cat main.go
package main

import(
        "github.com/go-arun/calculator/sum"
)

func main(){
        n1 := 10
        n2 := 10

        sum.Sum(n1,n2)
}

4.

root@debian1:/home/arun/lab# tree
.
|-- go.mod
|-- main.go
`-- sum
    `-- sum.go
Spokeshave answered 13/1, 2020 at 14:25 Comment(3)
If your module name is valid (starts with a domain) it shouldn't create any directories.Stoecker
"f1/f2/f3/f4/f5/hello" is the so called "import path" (the "name", the "identifier", the "handle") of your module. That name is pretty much unrelated to file system folders which is the whole point of a module build (in contrast to a GOPATH build).Flin
I got Popular Question badge today for this Question. But look at the votes , is funny !!Spokeshave
A
11

go mod init does not create those folders. You pass the "module path" to go mod init which is recorded in the go.mod file it creates.

The "module path" is the import path prefix corresponding to the module root. The module path and the relative path to the module root together form the complete import path which must be unique in an app.

So for example if your module contains a folder named foo (and a package foo in it), it is imported by a path being modulepath/foo. In your case it would be f1/f2/f3/f4/f5/hello/foo.

It is allowed for moduleA to contain a foo package, and also for moduleB to have a foo package. When used / imported, first would be imported like moduleA/foo the latter like moduleB/foo, so it's unambiguous which one you're importing. The module path is like a namespace.

It's recommended to use a module path that corresponds to a repository you plan or will publish your module to, so when you do, go get will be able to automatically fetch, build and install your module. For example you may choose a module path github.com/bob/hello, so when you publish your module, everyone can get it by simply using import "github.com/bob/hello" in their app.

Also note that you don't need to publish your code to a remote repo before you can build it. But it's still recommended to follow this pattern so you'll have less work to make it work in the future if you decide to publish it. Nothing to lose here.

More in the docs: Command go: Defining a module

Also: How to Write Go Code: Code organization

Arbogast answered 13/1, 2020 at 14:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.