Hello World in Go
Let's start with the traditional "Hello World" program. This simple program will help you understand the basic structure of a Go application.
Your First Go Program#
example.go
package main
import string">"fmt"
func main() {
fmt.Println(string">"Hello World")
}
Let's break down this program:
package main
: Every Go file starts with a package declaration. Themain
package is special - it defines an executable program rather than a library.import "fmt"
: This imports the format package from the standard library, which provides formatting functions.func main()
: The entry point of the program. Execution begins in themain
function.fmt.Println("Hello World")
: This prints the text "Hello World" to the console.
Running Your First Program#
To run this program, save it as hello.go
and execute the following command:
example.sh
go run hello.go
You should see Hello World
printed to your console.
Building an Executable#
You can also compile your program into a standalone executable:
example.sh
go build hello.go
This will create an executable file named hello
(or hello.exe
on Windows). You can run this executable directly:
example.sh
./hello
Understanding Go Program Structure#
Every Go program follows a similar structure:
- Package Declaration: All Go files begin with
package <name>
. Executable programs usepackage main
. - Import Statements: Import other packages that your code needs.
- Functions and Variables: Declare functions and variables. The
main()
function is required for executable programs. - Code Comments: Add documentation to your code using
//
for single-line comments or/* */
for multi-line comments.
Exercise#
Try modifying the "Hello World" program to:
- Print your name instead of "Hello World"
- Print multiple lines using multiple
fmt.Println()
calls - Add comments to explain what the program does
In the next module, we'll explore Go's variables and data types.