Master Go Basics: Program Structure, Execution, and Comments Explained
This article introduces Go’s basic program structure, how to run and build Go applications, the role of comments, why semicolons are omitted, and best practices for organizing imports, providing a concise foundation for writing clean Go code.
Go Program Structure
To understand Go program structure, start with a simple Hello, World! program:
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}Each source file has a .go extension and begins with a package declaration. In the example, package main indicates the file contains the entry point main function, making it an executable program. The import "fmt" brings in the standard formatting package for output.
Running Go Programs
Use go run or go build to execute Go code. go run compiles and runs the program directly, while go build produces an executable binary (e.g., hello-world) that can be run independently.
You can rename the binary with go build -o <file-name>. For installation, go install <package-name> creates a binary in $GOPATH/bin, which should be added to the system PATH to run from anywhere.
Code Comments
Adding comments improves code maintainability. Go supports line comments starting with // and block comments enclosed in /* ... */. Example:
// package main defines the entry point
package main
// import the 'fmt' package from standard library
import "fmt"
/*
The main function is the entry point in a Go program.
The main function does not have a return type.
Also, it does not accept any parameters.
*/Why No Semicolons
Go’s lexer automatically inserts semicolons based on simple rules, so they are omitted in source code. A semicolon is inserted after identifiers, literals, or tokens such as break, continue, return, etc., when a newline follows.
Revisiting Code Guidelines
Every standalone Go application must declare package main and contain a func main() entry point, similar to C, Java, or C#. Unlike Java or C#, the Go main function takes no arguments and returns nothing. When importing multiple packages, use a grouped import block:
import (
"context"
"database/sql"
"fmt"
"log"
)Summary
Go has a simple program structure resembling C, with package main and a func main() entry point. The language automatically handles semicolons, and supports both line and block comments for documentation. For deeper insight, consult the Effective Go page in the official Go documentation.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
21CTO
21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
