Hello World
Last updated
We'll delve into a simple yet fundamental Go program that prints "hello world" to the console.
package main
import "fmt"
func main() {
fmt.Println("hello world")
}hello worldLet's break down the code step by step:
package main: Every Go program starts with a package declaration. The main 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(): The main function is the entry point of every Go program. When the program starts, it's the main function that gets executed.
fmt.Println("hello world"): This line uses the Println function from the "fmt" package to print "hello world" followed by a newline to the console.
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.
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 named hello-world (or hello-world.exe on Windows).
Execute the binary by running ./hello-world (or hello-world.exe on Windows) in the terminal.
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