Skip to main content
St Louis

Back to all posts

How to Write And Run A Simple "Hello World" Program In Golang?

Published on
3 min read
How to Write And Run A Simple "Hello World" Program In Golang? image

Best Golang Programming Guides to Buy in October 2025

1 Learning Go: An Idiomatic Approach to Real-World Go Programming

Learning Go: An Idiomatic Approach to Real-World Go Programming

BUY & SAVE
$45.95 $65.99
Save 30%
Learning Go: An Idiomatic Approach to Real-World Go Programming
2 Go Programming Language, The (Addison-Wesley Professional Computing Series)

Go Programming Language, The (Addison-Wesley Professional Computing Series)

BUY & SAVE
$28.29 $49.99
Save 43%
Go Programming Language, The (Addison-Wesley Professional Computing Series)
3 100 Go Mistakes and How to Avoid Them

100 Go Mistakes and How to Avoid Them

BUY & SAVE
$39.16 $69.99
Save 44%
100 Go Mistakes and How to Avoid Them
4 Mastering Go: Leverage Go's expertise for advanced utilities, empowering you to develop professional software

Mastering Go: Leverage Go's expertise for advanced utilities, empowering you to develop professional software

BUY & SAVE
$29.27 $54.99
Save 47%
Mastering Go: Leverage Go's expertise for advanced utilities, empowering you to develop professional software
5 System Programming Essentials with Go: System calls, networking, efficiency, and security practices with practical projects in Golang

System Programming Essentials with Go: System calls, networking, efficiency, and security practices with practical projects in Golang

BUY & SAVE
$27.36 $41.99
Save 35%
System Programming Essentials with Go: System calls, networking, efficiency, and security practices with practical projects in Golang
6 Kubernetes in Action

Kubernetes in Action

BUY & SAVE
$31.00 $59.99
Save 48%
Kubernetes in Action
+
ONE MORE?

To write and run a simple "Hello World" program in Golang, follow these steps:

  1. Open a text editor or an integrated development environment (IDE) of your choice.
  2. Create a new file and save it with a ".go" extension, such as "hello.go".
  3. Start by importing the necessary package for printing: import "fmt". The "fmt" package provides various functions for formatting text in Go.
  4. Begin the main function using the keyword func main().
  5. Inside the main function, use the fmt.Println() function to print the desired output. In this case, we want to print "Hello, World!".
  6. Save the file.

Now, let's move on to running the program:

  1. Open a command prompt or terminal.
  2. Navigate to the directory where you saved the "hello.go" file.
  3. Compile the program by running go build hello.go. This will create an executable file named "hello" in the same directory.
  4. Finally, execute the program by typing ./hello (on Unix-based systems) or hello.exe (on Windows). Press Enter.

You should now see the output "Hello, World!" printed on the console. Congratulations, you have successfully written and run a simple "Hello World" program in Golang!

How to declare the main function in a Golang program?

In a Go program, the main function is declared as follows:

package main

func main() { // code... }

The main() function is the entry point for the program execution. It should be defined in the main package for an executable program. The code within the main() function is executed when the program starts running.

What is the function declaration syntax in Golang?

The function declaration syntax in Golang follows the pattern:

func functionName(parameter1 type, parameter2 type) return_type { // function body }

Here, func is the keyword used to declare a function. functionName is the name of the function, which should follow the same rules as naming variables. The parameters are specified with the parameter name followed by its type. Multiple parameters are separated by commas. The return type is specified after the parameters using the return_type keyword. If the function doesn't return any value, the return type is simply omitted.

For example, a function named add that takes two integers and returns their sum would be declared as:

func add(a int, b int) int { return a + b }

Note that the example specifies the type of each parameter and the return type as int. However, in Golang, if multiple consecutive parameters or return types share the same type, you can specify the type only once:

func add(a, b int) int { return a + b }

How to pass arguments to a Golang program?

In Go, command-line arguments can be passed to a program using the os package. The arguments are present in the os.Args slice, where the first element os.Args[0] contains the name of the program itself.

Here's an example of how to pass arguments to a Go program and access them:

package main

import ( "fmt" "os" )

func main() { // Accessing individual command-line arguments fmt.Println("Program Name:", os.Args[0]) fmt.Println("Arguments:")

for i, arg := range os.Args\[1:\] {
    fmt.Printf("Arg%d: %s\\n", i+1, arg)
}

}

If you save the above code to a file called main.go, you can run the program in the terminal with additional arguments like this:

go run main.go arg1 arg2 arg3

The program will then output:

Program Name: main Arguments: Arg1: arg1 Arg2: arg2 Arg3: arg3

You can replace go run main.go with go build to compile the program first and then run the compiled binary.