go

Can someone please dumb down go mod init for me?


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.


Solution

  • 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)
    }