Go First Program Hello World

Every programming journey starts with a simple program that displays a message on screen. In Go, this first program teaches the core structure every Go file follows. Understanding this one small program builds the foundation for everything else.

The Hello World Program

package main

import "fmt"

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

Output:

Hello, World!

Breaking Down Each Line

Line 1 – package main

package main

Every Go file belongs to a package. A package is simply a group of related code files. The special package named main tells Go that this file is the starting point of the program — the entry point. Every runnable Go program must have exactly one package main.

Line 2 – import "fmt"

import "fmt"

The import keyword brings in an external package. Here, fmt (short for "format") is a built-in Go package that handles printing text to the screen and reading input. Without importing it, the program cannot use fmt.Println.

Line 3 – func main()

func main() {

The word func declares a function. The function named main is special — Go automatically calls it first when the program runs. The opening curly brace { marks the start of the function body.

Line 4 – fmt.Println

    fmt.Println("Hello, World!")

fmt.Println prints a line of text to the screen and moves to the next line automatically. The text inside the double quotes is called a string literal — it is the exact text that appears in the output.

Line 5 – Closing Brace

}

The closing curly brace } ends the main function. Every opening brace must have a matching closing brace.

Program Flow Diagram

Program Starts
      │
      ▼
package main  ───►  Marks this as the entry point
      │
      ▼
import "fmt"  ───►  Loads the fmt package for printing
      │
      ▼
func main()   ───►  Go runs this function first
      │
      ▼
fmt.Println() ───►  Prints "Hello, World!" to screen
      │
      ▼
Program Ends

How to Run the Program

Step 1 – Create the file

Create a new file named main.go inside the project folder and paste the code.

Step 2 – Run the file

go run main.go

Step 3 – See the output

Hello, World!

Printing Multiple Lines

Multiple fmt.Println calls print each message on a separate line:

package main

import "fmt"

func main() {
    fmt.Println("Hello, World!")
    fmt.Println("Welcome to Go Programming")
    fmt.Println("Let us start learning!")
}

Output:

Hello, World!
Welcome to Go Programming
Let us start learning!

Print Without a New Line

fmt.Print works like fmt.Println but does not move to the next line automatically:

package main

import "fmt"

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

Output:

Hello, World!

Common Mistakes in the First Program

MistakeWhat HappensFix
Missing package mainGo does not know where to startAlways add it at the top
Missing import "fmt"Error: undefined fmtImport before using
Function name not mainProgram does not runEntry point must be named main
Single quotes instead of doubleSyntax errorStrings always use double quotes in Go

Key Points

  • package main marks the file as the program entry point
  • import "fmt" loads the formatting package needed for printing
  • func main() is the function Go runs first — always
  • fmt.Println prints text and adds a new line
  • fmt.Print prints text without moving to a new line
  • Strings in Go always use double quotes, never single quotes

Leave a Comment