Directory structure of a project

i am creating a file directory; there is main.go file in main folder but main.go gives this error when i run this file:

could not import mod1/title (no required module provides package “mod1/title”

mainfolder/
├── go.mod
├── go.sum
└── main.go
title/
└── ■■■.go

go.mod:

module mod1

go 1.19
package mainfolder

import (
    "mod1/title"
)

func main() {
    title.TitFunc()
}

move title under mainfolder

Can’t I just create the folders as I created them?

Don’t work against the tool. You also will need to change the package in main.go to main.

Generally speaking you will have 1 go module per repository / project. You could arrange your code how you have it, but that implies 2 modules which is almost certainly not what you want. What you PROBABLY want is something like this:

project/
├── go.mod
├── go.sum
├── main.go
├── /mainfolder/
└── /title/

You certain CAN have a module that references another module you maintain if it makes sense to separate them in that way. And you can also create a workspace if you must have multiple modules in a single project/repo (which as I mentioned is probably not what you want):

Also if you haven’t yet, check this out:

It contains a lot of useful, idiomatic advice on code organization.

1 Like

main.go and mod.go ,sum.go I want to put them in a folder too

You can do that using workspaces as I mentioned OR just create two projects if they really are completely different. For example charmbracelet/gum references other projects in the Charm organization:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.