Skip to main content
Go Language Fundamentals for Beginners to Advanced
CHAPTER 03 Beginner

Go Syntax and First Program

Updated: May 17, 2026
5 min read

# CHAPTER 3

Go Syntax and First Program

1. Introduction

Every great journey in programming begins with a single step: printing "Hello, World!" to the screen. In this chapter, we will write our first Go program. We will break down every single word of the code to understand exactly how Go structures its applications, packages, and functions.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Write and execute a basic Go program.
  • Understand the role of package main.
  • Import standard libraries like fmt.
  • Understand the entry point of a program: func main().
  • Use go run and go build commands.
  • Write single-line and multi-line comments.

3. Your First Go Program

Open VS Code, create a file named main.go, and type the following code exactly as shown:
go
1234567
package main

import "fmt"

func main() {
    fmt.Println("Hello, World!")
}

4. Step-by-Step Breakdown

Let's dissect this code line by line.

#### package main Every Go file must start with a package declaration. A package is a way to group related code together. The name main is special. It tells the Go compiler: "This is a standalone, executable program, not a shared library." If you name it package math, the compiler will build it as a library that other programs can use, but it won't run on its own.

#### import "fmt" Go has a minimalist philosophy. By default, it loads almost nothing into memory to stay fast. If you want to format text or print to the console, you must explicitly import the fmt (short for "format") package from the Go Standard Library.

#### func main() func is the keyword used to declare a function. The main() function is the Entry Point of your application. When you run your program, the operating system looks for func main() and starts executing code from there. (Note: The { must be on the same line as the function declaration, or Go will throw a syntax error!).

#### fmt.Println("Hello, World!") This calls the Println (Print Line) function from the fmt package we imported. It prints the text to the terminal and moves the cursor to the next line.

5. Running the Program

There are two ways to execute Go code from your terminal.

Method 1: go run (For Development)

bash
1
go run main.go

*Output:* Hello, World! This compiles the code in a temporary directory, runs it, and then deletes the temporary files. It feels like running an interpreted language (like Python), making development very fast.

Method 2: go build (For Production)

bash
1
go build main.go

This command compiles your code into a permanent, standalone binary executable file (main.exe on Windows, or just main on Mac/Linux). You can now run this binary directly:

bash
1
./main

*Output:* Hello, World! This binary contains everything it needs to run. You can email this file to a friend who doesn't even have Go installed, and it will run perfectly on their machine!

6. Comments in Go

Comments are text ignored by the compiler, used to leave notes for yourself or other developers.
go
12345678
// This is a single-line comment. It explains the code below.
fmt.Println("Welcome to Go!")

/*
This is a multi-line comment.
It can span across several lines.
Useful for long explanations or temporarily disabling chunks of code.
*/

7. Go Syntax Rules (Common Mistakes)

Go is strict about formatting. Here are the golden rules:
  • No Semicolons: Unlike C++ or Java, you do not need to put a semicolon ; at the end of a line. Go automatically inserts them behind the scenes during compilation.
  • Brace Placement: The opening curly brace { must be on the same line as the statement (like func main() {). If you put it on the next line, the code will fail to compile.
  • Unused Imports: If you write import "fmt" but never use it in your code, Go will refuse to compile! This strict rule prevents bloated, slow applications.

8. Mini Project: Simple Calculator CLI

Let's combine what we've learned to print some basic math.
go
12345678910111213141516
package main

import "fmt"

func main() {
    // Printing a welcome message
    fmt.Println("=== Simple Calculator ===")
    
    // Go can perform math directly inside the Println function
    fmt.Println("5 + 3 =", 5+3)
    fmt.Println("10 - 2 =", 10-2)
    fmt.Println("4 * 4 =", 4*4)
    fmt.Println("20 / 4 =", 20/4)
    
    fmt.Println("=========================")
}

9. Memory & Compilation Explanation

When you run go build, the Go compiler translates your human-readable text into raw binary instructions (1s and 0s) that the CPU understands directly. It also packages a tiny "Runtime" into the executable, which handles Garbage Collection (cleaning up memory) automatically while the program runs.

10. Best Practices

  • Use go fmt: Always format your code. If your braces or spaces look messy, run go fmt main.go in the terminal, and Go will instantly fix your spacing to match industry standards.

11. Exercises

  1. 1. Write a program that prints your name on the first line, and your favorite programming language on the second line.
  1. 2. Build the program using go build and run the executable directly from your terminal.

12. MCQs with Answers & Explanations

Question 1

What must every standalone, executable Go program start with?

Question 2

Which package is required to print text to the console?

Question 3

What is the entry point of a Go application?

Question 4

What does the go run command do?

Question 5

What does the go build command do?

Question 6

What happens if you import "fmt" but never use it in your code?

Question 7

Where must the opening curly brace { of a function be placed?

Q8. Does Go require semicolons (;) at the end of every statement? a) Yes b) No Answer: b) No. *Explanation: The compiler inserts them automatically.*

Q9. How do you write a single-line comment in Go? a) # Comment b) <!-- Comment --> c) // Comment d) /* Comment */ Answer: c) // Comment.

Question 10

How do you print a string and move the cursor to a new line?

13. Interview Preparation

Interview Questions:
  1. 1. Explain the difference between go run and go build. When would you use each?
  1. 2. Why is package main significant in a Go project?
  1. 3. What is the purpose of the fmt package?

Common Pitfall: Many developers coming from C++ or Java automatically press "Enter" before typing the { for a function. In an interview setting (like a coding whiteboard), explicitly mentioning that "Go requires the brace on the same line" shows you truly know the language syntax.

14. FAQs

Q: Can I have multiple main() functions in a project? A: No. A single executable program (a main package) can only have exactly one main() function.

Q: Do I need a runtime environment (like the JVM or Node.js) on the server to run the built executable? A: No! The output of go build is a static binary. You can drop it onto a blank Linux server and it will execute immediately.

15. Summary

Go programs are organized into packages. Executable programs must be in package main and must contain a func main() as the starting point. We use the fmt package to handle output, and tools like go run and go build to execute or compile our code. Go's strict formatting rules (like unused import errors and brace placement) enforce clean, readable code across all projects.

16. Next Chapter Recommendation

Now that you can write and run code, we need to learn how to store data in the computer's memory. In Chapter 4: Variables, Constants, and Data Types, we will explore how Go handles numbers, text, and boolean logic.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·