简体   繁体   中英

Where to place go.mod file

I have a repository structure as follows :-

xyz/src
      1. abc
            - p
            - q
            - r
      2. def
            - t
            - u
            - v
      3. etc
            - o
            - m
            - n

I have created a .mod file in src and run go build ./... Except for local packages everything is fine. So if abc/p is being used in def then it throws the following exception :- cannot find module providing package abc/p. The idea behind keeping the .mod file in src package was to make sure the path is being found from where the mod file is located. Can anyone suggest where should the mod file ideally should be? also i tried placing it one directory above in xyz but still same issue as well as i created one for each sub directory. I am bit confused on this. Will I have to create separate repository for abc and etc. But considering gopath which earlier used to work for the same I think module should also be able to do the same. Any suggestions?

I have a single go.mod in the root of my go application. I am using the following structure inspired by Kat Zien - How Do You Structure Your Go Apps

At the minute one of my applications looks like this

.
├── bin
├── cmd
│   ├── cli
│   └── server
│       └── main.go
├── pkg
│   ├── http
│   │   └── rest
|   │ # app-specific directories excluded
│   └── storage
│       └── sqlite

All packages are imported via their full path, ie import "github.com/myusername/myapp/pkg/http/rest" otherwise it causes problems all over the place and this was the one change I had to make going from $GOPATH to go mod .

go mod then handles all the dependencies it discovers properly as far as I've discovered so far.

The most common and easiest approach is a single go.mod file in your repository, where that single go.mod file is placed in the root of your repository .

Russ Cox commented in #26664 :

For all but power users, you probably want to adopt the usual convention that one repo = one module. It's important for long-term evolution of code storage options that a repo can contain multiple modules, but it's almost certainly not something you want to do by default.

The Modules wiki says :

For example, if you are creating a module for a repository github.com/my/repo that will contain two packages with import paths github.com/my/repo/foo and github.com/my/repo/bar , then the first line in your go.mod file typically would declare your module path as module github.com/my/repo , and the corresponding on-disk structure could be:

repo/
├── go.mod      <<<<< Note go.mod is located in repo root
├── bar
│   └── bar.go
└── foo
    └── foo.go

In Go source code, packages are imported using the full path including the module path. For example, if a module declared its identity in its go.mod as module github.com/my/repo , a consumer could do:

import "example.com/my/repo/bar"

That imports package bar from the module github.com/my/repo .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM