2

I have a Go module whose name is 'tatata'. It is only developed in my machine and thus it does not contain any path to any URL in the module name.

If I have only one main package in my module it works correctly. However, if I create two packages in the module, I am experiencing some problems.

This is the tree:

./|
  |- go.mod
  |-src
     |- main
     |   | - main.go
     |
     |- api
         | - api.go

The content of the files:

go.mod

module tatata

go 1.13

main.go

package main

import "fmt"
import "tatata/api"

func main() {
  fmt.Println("Hello world")
  api.Test1()
}

api.go

package api

import "fmt"
func Test1() {
   fmt.Println("Testing API")
}

When I try to build, I issue go build ./src/main and I get the following error: build tatata/src/main: cannot load tatata/api: malformed module path "tatata/api": missing dot in first path element

Now, if I try to put a dummy dot, like renaming the module to tatata.com I get the error build tatata.com/src/main: cannot load tatata.com/api: cannot find module providing package tatata.com/api as obviously my package/module is not available in that URL.

GOPATH is empty, i.e. not set.

Questions:

a) What is the right way, i.e. naming of modules and imports when using multiple packages inside a single Go module?

b) Is it impossible to have a Go module without dots in its name?!

c) Can you point me to any working example that has a building set-up with a single go module and multiple packages in the module?

Jose
  • 1,389
  • 3
  • 16
  • 25
  • 4
    You are missing the src segment: `import "tatata.com/src/api"`. – Peter Feb 20 '20 at 13:28
  • 2
    Start by removing the `src` folder. Such stuff does not belong into a module. And would make your import path valid. – Volker Feb 20 '20 at 13:45
  • There is a good overview of how to structure your module, packages, and files here: https://stackoverflow.com/a/57314494/11210494 – thepudds Mar 25 '22 at 20:50

3 Answers3

11

Can I have multiple packages inside a single go module?

Yes, of course.

How?

You have to do nothing, it just works.

(Your problem is: You try to import your api package by a wrong name. Import paths inside a module are of the form <modulename>/<relative-filepath-from-module-root>.)

Volker
  • 40,468
  • 7
  • 81
  • 87
  • Which means the import should have been: import "tatata/src/api" – Dan Ortega Oct 09 '20 at 16:31
  • The import statement format is different if the package is inside a directory and not in the same directory where module's go.mod file is present then I found following import format works import ( /). E.g. To use stringutils (which is inside directory string) in the module github.com/alessiosavi/GoGPUtils. Use import ( stringutils "github.com/alessiosavi/GoGPUtils/string") – Akshay Hiremath Feb 20 '22 at 22:59
3

You can have as many package as you want in a single module, have a look here: https://github.com/alessiosavi/GoGPUtils/

The only constraint is that you cane have a single package for every folder.

alessiosavi
  • 2,753
  • 2
  • 19
  • 38
1

I found the problem. The go.mod file has to be in the folder that contains the packages. In my case it was in the parent of that folder. Now working.

Jose
  • 1,389
  • 3
  • 16
  • 25