Getting Started with Go: Hello World

Go is a statically-typed, concurrent, and garbage-collected language that is designed for building fast and reliable applications. In this tutorial, we’ll start by writing a simple “Hello, World!” program in Go.

Here’s the code for a “Hello, World!” program in Go:

package main

import "fmt"

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

Let’s walk through the code line by line:

  • The first line, package main, specifies the package that this code belongs to. In Go, a package is a collection of related Go files. The main package is a special package that contains the entry point for the program, which is the main function.
  • The next line, import "fmt", imports the fmt package, which provides basic I/O functions, such as Println.
  • The main function is the entry point for the program and is called when you run the program. In this case, the main function calls the Println function from the fmt package to print the string “Hello, World!” to the console.

To run the program, you can save the code in a file named main.go and run the following command in the terminal:

go run main.go

This will compile and run the program, and you should see the output “Hello, World!” displayed in the terminal.

Congratulations, you’ve written your first Go program! From here, you can explore other features of the language and start building more complex programs.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *