Home  Golang   Hello world ...

Hello world program in Go (Golang)

Below is a simple "Hello, World!" program in Go:

package main

import "fmt"

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

Explanation:

  1. Package Declaration: Every Go file begins with a package declaration. The main package is special—it tells Go that this file is an executable program rather than a shared library.

  2. Import Statement: Imports are used to include packages that provide functionality beyond the basic language features. Here, we import the "fmt" package, which stands for "format" and provides functions for formatting input and output.

  3. Main Function: The main function is the entry point of any Go program. It is where execution of the program begins. In this example, main() calls fmt.Println() to print "Hello, World!" to the console.

  4. fmt.Println(): This function is used to print a line to the standard output (usually the console). It takes any number of arguments and prints them, separated by spaces, followed by a newline (\n).

Running the Program:

To run this program:

  1. Save the code to a file named hello.go (or any name ending with .go).

  2. Open a terminal or command prompt.

  3. Navigate to the directory where you saved hello.go.

  4. Run the program by typing:

    go run hello.go
    

    You should see the output:

    Hello, World!
    

Additional Notes:

Published on: Jun 19, 2024, 11:21 PM  
 

Comments

Add your comment