<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: beacon</title>
    <description>The latest articles on DEV Community by beacon (@open_dev).</description>
    <link>https://dev.to/open_dev</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4080859%2Fabdc56f7-5a3f-4d69-85d3-478c773e054d.png</url>
      <title>DEV Community: beacon</title>
      <link>https://dev.to/open_dev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/open_dev"/>
    <language>en</language>
    <item>
      <title>Understanding Main package and arguments passing in Go.</title>
      <dc:creator>beacon</dc:creator>
      <pubDate>Tue, 18 Aug 2026 11:19:44 +0000</pubDate>
      <link>https://dev.to/open_dev/understanding-main-package-and-arguments-passing-in-go-1a94</link>
      <guid>https://dev.to/open_dev/understanding-main-package-and-arguments-passing-in-go-1a94</guid>
      <description>&lt;p&gt;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.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;What is main package?&lt;/strong&gt;:
&lt;/h2&gt;

&lt;p&gt;Main package in Go is a special and mandatory package name used for stabdalone, executable program rather than a reuable library.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Significance of main package-&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Signals an executable program :&lt;/strong&gt; The Go compiler treats &lt;strong&gt;&lt;em&gt;package main&lt;/em&gt;&lt;/strong&gt; 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.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Defines the execution Entry point:&lt;/strong&gt; Every &lt;strong&gt;&lt;em&gt;package main&lt;/em&gt;&lt;/strong&gt; must contain a &lt;strong&gt;&lt;em&gt;func main()&lt;/em&gt;&lt;/strong&gt; function. This function takes no arguments, but you can access cli params through &lt;strong&gt;&lt;em&gt;os.Args&lt;/em&gt;&lt;/strong&gt; package which we will see later in the blog and returns nothing. &lt;strong&gt;&lt;em&gt;func main&lt;/em&gt;&lt;/strong&gt; servers as the absolute starting point where the program execution begins.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cannot be imported:&lt;/strong&gt; Because it represents a top-level application wrapper, other packages can't import the &lt;strong&gt;&lt;em&gt;package main&lt;/em&gt;&lt;/strong&gt;. If you try so, the compiler will throw an error.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: Reusable packages must contain a custom name (eg- package notmain).&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;What is os.Args? :&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;As we discussed earlier that &lt;strong&gt;&lt;em&gt;func main&lt;/em&gt;&lt;/strong&gt; can't not accept arguments and therefore you need another solution. Fortunately, go ships a built-in standard package called &lt;strong&gt;&lt;em&gt;package os&lt;/em&gt;&lt;/strong&gt; and this package has a built-in variable called &lt;strong&gt;&lt;em&gt;os.Args&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;os.Args&lt;/em&gt;&lt;/strong&gt; 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.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;os.Args&lt;/em&gt; has special structure-&lt;br&gt;
The elements in the slice follow a strict convention:&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;Code Example -&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;file name: main.go&lt;br&gt;
&lt;/p&gt;


&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s"&gt;"fmt"&lt;/span&gt;
    &lt;span class="s"&gt;"os"&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;userArg&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="n"&gt;finalStr&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="s"&gt;"Hello, "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;userArg&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"!"&lt;/span&gt;
        &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;finalStr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello, World!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;Run command&lt;/strong&gt; -&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;go run main.go &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;AND&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;go run main.go Alice&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;&lt;strong&gt;Expected Ouptput&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Hello, World!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;AND&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Hello, Alice!&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;&lt;strong&gt;Extras -&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;How does go decides if the first argument of &lt;strong&gt;&lt;em&gt;os.Args&lt;/em&gt;&lt;/strong&gt; should be name or file path?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  1. Called with a Relative Path
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Command:&lt;/strong&gt; &lt;code&gt;./myapp&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;os.Args[0]&lt;/code&gt;:&lt;/strong&gt; &lt;code&gt;./myapp&lt;/code&gt; &lt;em&gt;(contains the relative path)&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  2. Called with an Absolute Path
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Command:&lt;/strong&gt; &lt;code&gt;/home/user/bin/myapp&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;os.Args[0]&lt;/code&gt;:&lt;/strong&gt; &lt;code&gt;/home/user/bin/myapp&lt;/code&gt; &lt;em&gt;(contains the full absolute path)&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  3. Called via the System PATH (Just the Name)
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Command:&lt;/strong&gt; &lt;code&gt;myapp&lt;/code&gt; &lt;em&gt;(assuming it is in your system's PATH)&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;os.Args[0]&lt;/code&gt;:&lt;/strong&gt; &lt;code&gt;myapp&lt;/code&gt; &lt;em&gt;(contains just the command name)&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  4. Invoked via a Symlink
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Behavior:&lt;/strong&gt; If you create a symbolic link pointing to your binary and run the symlink name, &lt;code&gt;os.Args[0]&lt;/code&gt; will contain the &lt;strong&gt;name or path of the symlink&lt;/strong&gt;, not the target binary.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;strong&gt;Summary-&lt;/strong&gt;&lt;br&gt;
As we saw, &lt;strong&gt;&lt;em&gt;package main&lt;/em&gt;&lt;/strong&gt; 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 &lt;strong&gt;os&lt;/strong&gt; package to retrieve them.&lt;/p&gt;




</description>
      <category>go</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
