Corporate Training
Request Demo
Click me
Menu
Let's Talk
Request Demo

Tutorials

Your First Go Program

3. Your First Go Program

A simple example of a "Hello, World!" program written in Go:

package main

import "fmt"

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

Let's break down what's happening in this program:

  • The 'package main' statement is used to indicate that this file belongs to the main package. The 'main' package is special in Go and is used for executable programs.
  • The 'import "fmt"' statement imports the "fmt" package, which provides functions for formatted I/O (input/output). The'fmt.Println'function is used to print a line of text to the standard output (usually the terminal).
  • The 'func main()' is the entry point of the program. When you run a Go program, execution starts from the'main' function. It doesn't take any arguments, and its body is enclosed in curly braces '{}'.
  • Inside the 'main' function, the 'fmt.Println("Hello, World!")'line calls the'Println'function from the 'fmt'package to print the "Hello, World!" message to the standard output.

Running the Program:

  • Save the program in a file named 'main.go'.
  • Open a terminal and navigate to the directory where 'main.go' is located.
  • Run the program using the 'go run' command:
go run main.go

 

  • You should see the output: "Hello, World!" printed to the terminal.
Hello, World!

 

Congratulations! You've just created and run your first Go program. This simple example demonstrates the basic structure of a Go program and how to use the 'fmt' package for printing. From here, you can start exploring more advanced features of the language and build more complex applications.