Go file not running which is not in main package
Asked Answered
S

2

17

I have a very simple Go project setup. At root directory I have go.mod file and main.go and a folder called main2. Inside main2 folder there is main2.go file.

/
|_ go.mod
|_ main.go
|_ main2
   |_ main2.go

From root directory I am trying to run go run command

go run main2/main2.go

and it throws error:

package command-line-arguments is not a main package

Can someone help?

Stiffen answered 28/4, 2022 at 14:58 Comment(4)
Never use go run with filename arguments.Southwards
@Southwards An explanation would be more helpful than a cryptic injunction.Garica
@Garica go run path/to/file.go implies whatever file you're passing is the main package, and has a main function. If your main package(s) are somewhere in cmd/foo/main.go, it's highly likely you're either importing other packages, or there are other files in the main package containing functions that are being called from main. Say cmd/foo/main.go calls funcitons in cmd/foo/helpers.go, that file is not passed to go run, and thus the functions will be undefined, and go run will error. go run compiles and runs, so just go build ./cmd/foo and run the binary, easy and saferRetaliation
The package main statement is how we tell Go that the app we're creating is an executable program (a file you can run). Every executable app has this first line, even if the project or file has a different name. And if you run the go build, a binary executable isn't produced.Coalfield
I
17

The package of your main2.go file must be main. When there is a main package, and a function main in your project, the compiler knows it will be compiled as a executable, and not as a library.

So try to change package command-line-arguments to package main inside the main2/main2.go file.

Izak answered 28/4, 2022 at 15:8 Comment(2)
Does it mean that we can only run main function of main package only and we can't run any other function of any other package other than main?Stiffen
@mohitsingla You can create multiple functions in each package, and multiple packages. But to define the program entry point, you have to define a main package, and inside it you have to define a main function. See: go.dev/tour/basics/4Izak
P
9

Imagine a golang executable as a house with only a front door and many different rooms inside it. You can go through any door you want once you are in the house, but to get inside you have to go through the front door first. That front door is the main() function.

Golang's entry point into an executable is through main(). If you want to run different logic paths for a single executable, you can use main() as a routing function to the other packages using command line arguments:

package main

import (
    "os"
    "otherpackage"
    // Your child packages get imported here.
)

func main() {

    // The first argument
    // is always program name
    // So os.Args[1] is the first dynamic argument
    arg1 := os.Args[1]

    // use arg1 to decide which packages to call
    if arg1 == "option1" {
        // option1 code executes here.
        otherpackage.DoThis()
    }
    if arg1 == "option2" {
        // option2 code executes here.
        otherpackage.DoThat()
    }
}

Then you can run your program with something like:

go run main.go option1

From golang documentation:

Program execution A complete program is created by linking a single, unimported package called the main package with all the packages it imports, transitively. The main package must have package name main and declare a function main that takes no arguments and returns no value.

Polychasium answered 28/4, 2022 at 15:21 Comment(2)
Does it mean that we can only run main function of main package only and we can't run any other function of any other package other than main?Stiffen
You can still run those other functions, but they have to be called somewhere along the call stack that originates from main() in package main.Polychasium

© 2022 - 2024 — McMap. All rights reserved.