Sredny M Casanova
Sredny M Casanova

Reputation: 5063

Correct structure of Golang repository

I am starting in Golang development. Right now my boss gave me the repository of a project that other developer made, now he's gone of the company and I am not able to ask him some things related to it. Right now I have a confussion about the project structure that he pushed to the repo, the structure is the next:

 |-MyApp
 |--bin
 |--pkg
 |--src
 |----api (the code of the app)
 |----github.com
 |----golang.org
 |----gopkg.in

To me, it's exaclty as the estructure of the Go, 1.- in the repo should not be only the api folder? If I go to the api folder and make go run main.go I get a message that some packages are not found even when they are in the folder, 2.-how I specify the packages in the go run command?

3.- Is a good practice to set this kind of structure for the golang projects? I see in the code that he exported the packages only with "package1", if I copy and paste the code of the app inside the golang workspace then I have to specify the name of the folder to export the packages, example: "myApp/package1" so there I have that doubt. Thank you

Upvotes: -2

Views: 807

Answers (2)

codeScoop
codeScoop

Reputation: 11

Why Packages Are Not Found?

When you run go run main.go inside the api folder, Go can't resolve imports because the project is not properly set up with Go Modules.

What to do:

Check for a go.mod file in the root directory. If it doesn't exist, you need to initialize the module:

go mod init myapp

Replace myapp with the name of your module.

Ensure that dependencies are properly listed in the go.mod file. If not, run:

go mod tidy

Fix import paths in the code:

If the api folder is the root of your project, imports should use the module name (e.g., myapp/package1).

Specifying Packages in go run To run your application, use the go run command at the root of the module (where go.mod resides):

go run ./api/main.go

If dependencies are missing or not found, ensure go mod tidy is run to download and set them up.

Is This a Good Practice? The structure you described is not recommended for modern Go development. The current best practice for Go projects is to use a flat and simple structure with Go Modules.

Recommended Structure:


|-MyApp
 |--api/        # Application code
 |----main.go   # Entry point
 |----package1/ # Sub-packages
 |----package2/ # Sub-packages
 |--go.mod      # Go Modules file
 |--go.sum      # Dependency checksums

Key Points:

Go Modules:

Dependencies are managed via go.mod and go.sum. No need to include src, github.com, golang.org, or gopkg.in in your repo. Flat Structure:

Avoid unnecessary nesting (src or bin folders are redundant for most projects). Keep code and dependencies clearly separated. Imports:

Use proper module-based imports. For example: If your module is myapp, import packages as myapp/package1. Steps to Refactor the Project Move the api folder to the root of the repository:

MyApp/
├── api/
├── go.mod

Initialize the module (if not done yet):

go mod init myapp
go mod tidy
// Instead of this:
import "package1"
// Use:
import "myapp/package1"

Run the application:

go run ./api/main.go

Why Modules are Better No Manual Dependency Management: Go Modules fetch and manage dependencies automatically. Portable and Clean: Your repo only needs your code (api folder), go.mod, and go.sum. No vendor directories or dependency code.

Upvotes: 0

Volker
Volker

Reputation: 42448

That all depends. There is not a single right way for everything.

It seems as if this repo decided to vendor everything, the whole GOPATH. That is okay but with the "modern" vendor folder today one would do it probably differently.

Never do go run. That's for toy programs only. Build your software with go build and go install. These command work on package path.

For everything else: Please see the official documentation https://golang.org/doc/code.html and stick to it (which means you should move stuff around a bit).

Upvotes: 2

Related Questions