Go Syntax and First Program
# 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 runandgo buildcommands.
- Write single-line and multi-line comments.
3. Your First Go Program
Open VS Code, create a file namedmain.go, and type the following code exactly as shown:
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)
*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)
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:
*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.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 (likefunc 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.9. Memory & Compilation Explanation
When you rungo 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, rungo fmt main.goin the terminal, and Go will instantly fix your spacing to match industry standards.
11. Exercises
- 1. Write a program that prints your name on the first line, and your favorite programming language on the second line.
-
2.
Build the program using
go buildand run the executable directly from your terminal.
12. MCQs with Answers & Explanations
What must every standalone, executable Go program start with?
Which package is required to print text to the console?
What is the entry point of a Go application?
What does the go run command do?
What does the go build command do?
What happens if you import "fmt" but never use it in your code?
Where must the opening curly brace { of a function be placed?
Q9. How do you write a single-line comment in Go? a) # Comment b) <!-- Comment --> c) // Comment d) /* Comment */ Answer: c) // Comment.
How do you print a string and move the cursor to a new line?
13. Interview Preparation
Interview Questions:-
1.
Explain the difference between
go runandgo build. When would you use each?
-
2.
Why is
package mainsignificant in a Go project?
-
3.
What is the purpose of the
fmtpackage?
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 multiplemain() 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 intopackages. 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.