Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it ok to put the one main package in a cmd/myapp subdirectory?

Tags:

go

this is my first Go project and I have had a hard time grasping the different documents on code organization (e.g. I found it somewhat hard to tell which ones have been superceded, or apply to deprecated vendoring tools/approaches). I finally settled on Ben Johnson's Standard Package Layout, with a very slim root package (in the root directory of my project) and most code in internal/pkg/...

However, I've added a cmd/myapp dir and put my main.go file for the executable program in there, since I understood that to be a common pattern.

So now I have:

myapp
|
+- myapp.go   // "package myapp", with only type and interface declarations and no imports
|
+- cmd
|   |
|   +- main.go // "package main", with import gitlab.tld/username/myapp, gitlab.tld/username/myapp/internal/pkg/routing etc.
|
+- internal
    |
    +- pkg
        |
        +- routing
        |    |
        |    +- routing.go // "package routing"
        |
        +- sql 
        |
        +- etc.

Now, when I go to cmd/myapp and call go build, it builds fine and leaves an executable that does what it's supposed to do right there. But when I call go build -v from the project's main directory, no executable is produced. In fact, only myapp.go seems to be processed which contains only type definitions. (And of course, go install does not produce a binary either. go get -v gitlab.tld/username/myapp also does not seem to do anything.)

So I have the suspicion that this is not how it is meant after all, but I am not even so sure about that. Is there a problem with the project layout, or did I get the usage of go tooling wrong?

Thanks for any help you might provide to this go newbie,

Andreas

like image 936
awagner Avatar asked Jan 30 '18 10:01

awagner


People also ask

Can we have multiple main methods in golang?

Yes, thats totally fine. For go build or go run you then provide the path to the main file you want to compile.

What is Golang CMD package?

Package cmd runs external commands with concurrent access to output and status. It wraps the Go standard library os/exec. Command to correctly handle reading output (STDOUT and STDERR) while a command is running and killing a command. All operations are safe to call from multiple goroutines.

How do I run multiple files at once?

If you are trying to run multiple files on localhost using gorilla mux in go as per latest version(1.11). Try using any of the following 2 commands. go install && FolderName -port 8081 . go build && ./FolderName -port 8081.

What is CMD folder in Golang?

/cmd. This folder contains the main application entry point files for the project, with the directory name matching the name for the binary. So for example cmd/simple-service meaning that the binary we publish will be simple-service .


1 Answers

When you run go build or go install the command expects a list of packages to process. If no import paths are given, the action applies to the package in the current directory.

So if there is no package main in your root directory, no executable will be produced.

The command doesn't look in subdirectories. You may have multiple commands in your cmd directory. In this case it won't know which one to build. And building all of them every time is wasteful.

like image 66
dev.bmax Avatar answered Nov 15 '22 04:11

dev.bmax