<?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: Sahal Mahfudh</title>
    <description>The latest articles on DEV Community by Sahal Mahfudh (@saahalla).</description>
    <link>https://dev.to/saahalla</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%2F4136773%2Fb519ebe4-e8d3-49af-a3f7-0a4fc99a1887.jpg</url>
      <title>DEV Community: Sahal Mahfudh</title>
      <link>https://dev.to/saahalla</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/saahalla"/>
    <language>en</language>
    <item>
      <title>Getting Started with Go: go run and go build</title>
      <dc:creator>Sahal Mahfudh</dc:creator>
      <pubDate>Wed, 23 Sep 2026 08:20:29 +0000</pubDate>
      <link>https://dev.to/saahalla/getting-started-with-go-go-run-and-go-build-2k9h</link>
      <guid>https://dev.to/saahalla/getting-started-with-go-go-run-and-go-build-2k9h</guid>
      <description>&lt;p&gt;Go is a compiled language. This means your human-readable code must be translated into machine code before the computer can execute it.&lt;/p&gt;

&lt;p&gt;When you start your journey with Go, the two most important commands you will use daily are &lt;code&gt;go run&lt;/code&gt; and &lt;code&gt;go build&lt;/code&gt;. While they might seem similar at first glance, they serve entirely different purposes in the software development lifecycle.&lt;/p&gt;

&lt;p&gt;Here is a breakdown of how and when to use them.&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating a Sample Project
&lt;/h3&gt;

&lt;p&gt;Before testing these commands, you need to set up a basic Go project. Open your terminal, create a new directory for your project, and initialize a Go module:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;➔ &lt;span class="nb"&gt;mkdir &lt;/span&gt;golang-project
➔ &lt;span class="nb"&gt;cd &lt;/span&gt;golang-project
➔ go mod init golang-project
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, create a file named main.go inside that folder and add the following code:&lt;br&gt;
&lt;/p&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="s"&gt;"fmt"&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="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;"go is running"&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;h3&gt;
  
  
  1. go run
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;go run&lt;/code&gt; command is built for quick testing while you write code. When you execute &lt;code&gt;go run main.go&lt;/code&gt;, Go works behind the scenes to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Compile your code into a temporary file.&lt;/li&gt;
&lt;li&gt;Run the program immediately.&lt;/li&gt;
&lt;li&gt;Delete the temporary file as soon as the program finishes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;How to use:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;➔ go run main.go
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;go is running
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;When to use go run:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Active Development: When you are writing code and want to quickly test if your logic works.&lt;/li&gt;
&lt;li&gt;Testing Scripts: When you are using Go to write quick, script-like tools where you don't need a permanent executable file cluttering up your directory.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Because &lt;code&gt;go run&lt;/code&gt; has to compile the code every single time you execute it, it is not meant for production environments.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. go build
&lt;/h3&gt;

&lt;p&gt;Use go build when your application is finished and ready to deploy. When you execute this command, Go will:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Compile your code into a permanent, standalone executable file.&lt;/li&gt;
&lt;li&gt;Bundle everything your program needs into that single file.&lt;/li&gt;
&lt;li&gt;Allow you to run the program on other computers without needing to install Go.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;How to use:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;➔ go build
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After running this command, you will see a new executable file in your directory. On Linux or macOS, it will be named &lt;code&gt;golang-project&lt;/code&gt;, and on Windows, it will be &lt;code&gt;golang-project.exe&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;➔ &lt;span class="nb"&gt;ls
&lt;/span&gt;go.mod   golang-project   main.go
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Changing the Executable Name:&lt;/strong&gt;&lt;br&gt;
By default, the binary or executable file name matches your project name. To change the name of the executable file, use the -o flag:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;➔ go build &lt;span class="nt"&gt;-o&lt;/span&gt; my-custom-app
➔ &lt;span class="nb"&gt;ls
&lt;/span&gt;go.mod         golang-project main.go        my-custom-app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Running &lt;code&gt;go build -o my-custom-app&lt;/code&gt; generates the &lt;code&gt;my-custom-app&lt;/code&gt; file directly in your project directory.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to run the built binary:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# On Linux / macOS&lt;/span&gt;
➔ ./golang-project
go is running

&lt;span class="c"&gt;# On Windows&lt;/span&gt;
golang-project.exe

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;When to use go build:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Production Deployment: When you are ready to ship your application to a server or to your users.&lt;/li&gt;
&lt;li&gt;Performance: Because the code is already compiled, running the binary is extremely fast.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Cross-Compilation with go build&lt;/strong&gt;&lt;br&gt;
Go has a great feature called cross-compilation, which lets you build an app for a completely different operating system right from your current computer.&lt;/p&gt;

&lt;p&gt;Just change the &lt;code&gt;GOOS&lt;/code&gt; (Operating System) and &lt;code&gt;GOARCH&lt;/code&gt; (Architecture) variables when building.&lt;/p&gt;

&lt;p&gt;Here is the complete list of simple commands for the major operating systems:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Building for Windows
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;GOOS&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;windows &lt;span class="nv"&gt;GOARCH&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;amd64 go build &lt;span class="nt"&gt;-o&lt;/span&gt; my-app.exe
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Building for Linux
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;GOOS&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;linux &lt;span class="nv"&gt;GOARCH&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;amd64 go build &lt;span class="nt"&gt;-o&lt;/span&gt; my-app-linux
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Building for macOS (Apple Silicon)
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;GOOS&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;darwin &lt;span class="nv"&gt;GOARCH&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;arm64 go build &lt;span class="nt"&gt;-o&lt;/span&gt; my-app-mac-m1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Building for macOS (Older Intel Macs)
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;GOOS&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;darwin &lt;span class="nv"&gt;GOARCH&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;amd64 go build &lt;span class="nt"&gt;-o&lt;/span&gt; my-app-mac-intel
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Summary Comparison
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
        &lt;thead&gt;
            &lt;tr&gt;
                &lt;th&gt;Feature&lt;/th&gt;
                &lt;th&gt;&lt;code&gt;go run&lt;/code&gt;&lt;/th&gt;
                &lt;th&gt;&lt;code&gt;go build&lt;/code&gt;&lt;/th&gt;
            &lt;/tr&gt;
        &lt;/thead&gt;
        &lt;tbody&gt;
            &lt;tr&gt;
                &lt;td&gt;&lt;strong&gt;Primary Use&lt;/strong&gt;&lt;/td&gt;
                &lt;td&gt;Local development and testing&lt;/td&gt;
                &lt;td&gt;Production deployment and distribution&lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr&gt;
                &lt;td&gt;&lt;strong&gt;Creates a File?&lt;/strong&gt;&lt;/td&gt;
                &lt;td&gt;No (uses a hidden temporary file)&lt;/td&gt;
                &lt;td&gt;Yes (creates a permanent binary executable)&lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr&gt;
                &lt;td&gt;&lt;strong&gt;Execution&lt;/strong&gt;&lt;/td&gt;
                &lt;td&gt;Compiles and runs automatically&lt;/td&gt;
                &lt;td&gt;Only compiles; you must run the file manually&lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr&gt;
                &lt;td&gt;&lt;strong&gt;Speed&lt;/strong&gt;&lt;/td&gt;
                &lt;td&gt;Slower (compiles every time)&lt;/td&gt;
                &lt;td&gt;Extremely fast (already compiled)&lt;/td&gt;
            &lt;/tr&gt;
        &lt;/tbody&gt;
    &lt;/table&gt;&lt;/div&gt;

&lt;p&gt;By understanding the difference between these two commands, you will be well on your way to developing and deploying efficient Go applications.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Reference:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://go.dev/doc/tutorial/getting-started" rel="noopener noreferrer"&gt;https://go.dev/doc/tutorial/getting-started&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://go.dev/wiki/WindowsCrossCompiling" rel="noopener noreferrer"&gt;https://go.dev/wiki/WindowsCrossCompiling&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pkg.go.dev/cmd/go#hdr-Compile_packages_and_dependencies" rel="noopener noreferrer"&gt;https://pkg.go.dev/cmd/go#hdr-Compile_packages_and_dependencies&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dasarpemrogramangolang.novalagung.com/A-go-command.html" rel="noopener noreferrer"&gt;https://dasarpemrogramangolang.novalagung.com/A-go-command.html&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>go</category>
      <category>basic</category>
      <category>programming</category>
      <category>dolearncode</category>
    </item>
  </channel>
</rss>
