Hello World
We'll delve into a simple yet fundamental Go program that prints "hello world" to the console.
Code Breakdown
Output
Let's break down the code step by step:
package main
: Every Go program starts with apackage
declaration. Themain
package is special; it indicates that this is an executable program rather than a library.import "fmt"
: This line imports the "fmt" package, which stands for format. It provides functions for formatting input and output, including printing to the console.func main()
: Themain
function is the entry point of every Go program. When the program starts, it's themain
function that gets executed.fmt.Println("hello world")
: This line uses thePrintln
function from the "fmt" package to print "hello world" followed by a newline to the console.
Running the Program 🏃
To run this program, follow these steps:
Ensure you have Go installed on your system.
Copy the code into a file with a ".go" extension, for example,
hello-world.go
.Open a terminal and navigate to the directory containing your file.
Run the command
go run hello-world.go
.You should see the output:
hello world
.
Building and Running as a Binary ⚙️
Alternatively, you can build the program into a binary and run it separately:
In the same directory as your code, run
go build hello-world.go
. This will create an executable file namedhello-world
(orhello-world.exe
on Windows).Execute the binary by running
./hello-world
(orhello-world.exe
on Windows) in the terminal.
Conclusion
Congratulations! 🎉 You've just created and executed your first Go program. This simple "Hello World" example sets the stage for exploring more advanced concepts in Go.
Last updated
Was this helpful?