Go에서 전달되는 주요 패키지 및 인수 이해하기.

작성자

카테고리:

← 피드로
DEV Community · beacon · 2026-08-18 개발(SW)

beacon

This blog covers Main package and its significance. Entry point of Golang program. How to pass argument when starting a program with os.Args package.

What is main package?:

Main package in Go is a special and mandatory package name used for stabdalone, executable program rather than a reuable library.

Significance of main package-

  • Signals an executable program : The Go compiler treats package main differently than other packages. It tells the compiler to compile the code into a standalone executable file (eg- .exe on windows or a binary on Unix/Linux) rather than an importable shared library.
  • Defines the execution Entry point: Every package main must contain a func main() function. This function takes no arguments, but you can access cli params through os.Args package which we will see later in the blog and returns nothing. func main servers as the absolute starting point where the program execution begins.
  • Cannot be imported: Because it represents a top-level application wrapper, other packages can’t import the package main. If you try so, the compiler will throw an error.

Note: Reusable packages must contain a custom name (eg- package notmain).

What is os.Args? :

As we discussed earlier that func main can’t not accept arguments and therefore you need another solution. Fortunately, go ships a built-in standard package called package os and this package has a built-in variable called os.Args.

os.Args is a slice of strings ([]string) that holds the command-line arguments passed to the program when it was executed from the terminal or command prompt.

os.Args has special structure-
The elements in the slice follow a strict convention:

  • os.Args[0]: Always constains the path or name of the executable file itself (name or path depends on how the program was executed, read more about it below).
  • os.Args[1]: The first command-line argument provided by the user.
  • os.Args[2]: The second command-line argument.
  • os.Args[nth]: The nth command-line argument (where n is any index of the total counts of arguments passed).

Code Example –

file name: main.go

package main

import (
    "fmt"
    "os"
)

func main() {
    if len(os.Args) > 1 {
        userArg := os.Args[1]
        finalStr := "Hello, " + userArg + "!"
        fmt.Println(finalStr)
    } else {
        fmt.Println("Hello, World!")
    }

} 

Enter fullscreen mode Exit fullscreen mode

Run command

go run main.go

AND

go run main.go Alice

Expected Ouptput

Hello, World!

AND

Hello, Alice!

Extras –

How does go decides if the first argument of os.Args should be name or file path?

1. Called with a Relative Path

  • Command: ./myapp
  • os.Args[0]: ./myapp (contains the relative path)

2. Called with an Absolute Path

  • Command: /home/user/bin/myapp
  • os.Args[0]: /home/user/bin/myapp (contains the full absolute path)

3. Called via the System PATH (Just the Name)

  • Command: myapp (assuming it is in your system’s PATH)
  • os.Args[0]: myapp (contains just the command name)

4. Invoked via a Symlink

  • Behavior: If you create a symbolic link pointing to your binary and run the symlink name, os.Args[0] will contain the name or path of the symlink, not the target binary.

Summary-
As we saw, package main handled uniquely by Go, it signifies that the pacakge is a standalone executable and it has some aspects and has a few quirks, like not being able to take arguments directly and needing the os package to retrieve them.

원문에서 계속 ↗