Introduction

Writing a "Hello, World!" program is a common starting point for learning any programming language. In this guide, we will walk you through the process of creating your first Go program, explaining the code step by step.


Create a New File

Begin by creating a new file for your Go program. You can use any text editor or integrated development environment (IDE) of your choice. Save the file with a .go extension. Let's name it hello.go.


Write Your Go Code

Now, open hello.go in your editor and write the following Go code:

                            package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}

Let's break down this code:

  • package main: Every Go program must start with a package declaration. The main package is the entry point for an executable program.
  • import "fmt": This line imports the "fmt" package, which provides functions for formatted input and output. We use it to print "Hello, World!" to the console.
  • func main(): The main() function is the entry point of your program. It's where the program execution begins.
  • fmt.Println("Hello, World!"): This line prints the string "Hello, World!" to the standard output (usually the console).

Run Your Program

To run your Go program, open a terminal, navigate to the directory where hello.go is located, and execute the following command:

                            $ go run hello.go

If everything is set up correctly, you should see "Hello, World!" printed to the console.


Congratulations!

You've successfully written and executed your first Go program. "Hello, World!" is a simple example, but it's an important first step in your journey to becoming a Go programmer. You can now start exploring the language and building more complex applications.


Next Steps and Resources

To continue your Go programming journey, here are some resources to help you get started: