Hello World

We'll delve into a simple yet fundamental Go program that prints "hello world" to the console.

Code Breakdown

package main

import "fmt"

func main() {
    fmt.Println("hello world")
}

Output

hello world

Let'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.

Running the Program πŸƒ

To run this program, follow these steps:

  1. Ensure you have Go installed on your system.

  2. Copy the code into a file with a ".go" extension, for example, hello-world.go.

  3. Open a terminal and navigate to the directory containing your file.

  4. Run the command go run hello-world.go.

  5. 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:

  1. 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).

  2. Execute the binary by running ./hello-world (or hello-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?