Getting Started with Go: Hello World and Basic Syntax
This article introduces the classic “Hello, World!” program in Go, explains the language’s basic syntax—including package declaration, imports, functions, variables, constants, data types, control structures, loops, and comments—and provides instructions for running and formatting Go code.
"Hello, World!" is the first milestone when learning any programming language. This simple program not only verifies that your development environment is set up correctly, but also quickly introduces the basic structure of the language.
First Go Program: Hello, World!
Let's start with the simplest Go program:
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}Code Analysis
package main : Every Go file must begin with a package declaration.
import "fmt" : Imports the standard library fmt package for formatted I/O.
func main() : The main function is the entry point of the program; Go automatically calls it when the program runs.
fmt.Println("Hello, World!") : Calls the Println function of the fmt package to print "Hello, World!" to the console with a newline.
Run the Program
To run this program, you need:
Save the code as hello.go .
Execute in a terminal: go run hello.go
Or compile first and then run: go build hello.go ./hello
Go Language Basic Syntax
1. Variable Declaration
Go is a statically typed language; variables must have an explicit type.
// Explicit type declaration
var name string = "Go语言"
// Type inference
var age = 25
// Short declaration (only inside functions)
score := 1002. Constants
Use the const keyword to declare constants:
const Pi = 3.14159
const (
StatusOK = 200
StatusNotFound = 404
)3. Basic Data Types
Go's basic data types include:
Boolean: bool (true/false)
Numeric: Integers: int , int8 , int16 , int32 , int64 Unsigned integers: uint , uint8 , uint16 , uint32 , uint64 Floating‑point: float32 , float64 Complex numbers: complex64 , complex128
String: string
Byte: byte (alias of uint8 )
Rune: rune (alias of int32 , represents a Unicode code point)
4. Control Structures
Conditional Statements
// if‑else
if x > 10 {
fmt.Println("x is greater than 10")
} else if x == 10 {
fmt.Println("x is equal to 10")
} else {
fmt.Println("x is less than 10")
}
// Simple statement before condition
if value := getValue(); value > 100 {
fmt.Println("Value is large")
}Loops
Go only has the for loop; there is no while or do‑while construct.
// Traditional for loop
for i := 0; i < 10; i++ {
fmt.Println(i)
}
// While‑style loop replacement
sum := 1
for sum < 1000 {
sum += sum
}
// Infinite loop (break needed to exit)
for {
// do something
break
}5. Functions
Basic function syntax:
// Simple function
func add(a int, b int) int {
return a + b
}
// Multiple return values
func swap(x, y string) (string, string) {
return y, x
}
// Named return values
func split(sum int) (x, y int) {
x = sum * 4 / 9
y = sum - x
return // naked return
}6. Comments
Go supports two comment styles:
// Single‑line comment
/*
Multi‑line
comment
*/Code Style Considerations
Formatting: Go enforces a strict style; use the go fmt command to automatically format code.
Semicolons: The compiler inserts semicolons automatically; manual insertion is rarely needed.
Braces: The opening brace { must be on the same line as the statement.
Visibility: Identifiers that start with an uppercase letter are exported (public); those starting with a lowercase letter are package‑private.
Conclusion
Through this simple "Hello, World!" program we have learned the basic structure of a Go program and some fundamental syntax. Go is designed to be concise and its syntax is clear, making it ideal for quick onboarding.
In the next lessons we will explore more advanced Go features, including composite data types, pointers, methods, and interfaces.
Remember, practice is the best way to learn programming. Try modifying this simple program and adding more functionality to accelerate your mastery of Go.
Java learning materials
C language learning materials
Frontend learning materials
C++ learning materials
PHP learning materials
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
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.