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

7 minutes read

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!

Best Golang Books to Read in 2024

1
Mastering Go: Create Golang production applications using network libraries, concurrency, machine learning, and advanced data structures, 2nd Edition

Rating is 5 out of 5

Mastering Go: Create Golang production applications using network libraries, concurrency, machine learning, and advanced data structures, 2nd Edition

2
Distributed Services with Go: Your Guide to Reliable, Scalable, and Maintainable Systems

Rating is 4.9 out of 5

Distributed Services with Go: Your Guide to Reliable, Scalable, and Maintainable Systems

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

Rating is 4.8 out of 5

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

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

Rating is 4.7 out of 5

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

5
Event-Driven Architecture in Golang: Building complex systems with asynchronicity and eventual consistency

Rating is 4.6 out of 5

Event-Driven Architecture in Golang: Building complex systems with asynchronicity and eventual consistency

6
Hands-On Software Architecture with Golang: Design and architect highly scalable and robust applications using Go

Rating is 4.5 out of 5

Hands-On Software Architecture with Golang: Design and architect highly scalable and robust applications using Go

7
Head First Go

Rating is 4.4 out of 5

Head First Go


How to declare the main function in a Golang program?

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

1
2
3
4
5
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:

1
2
3
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:

1
2
3
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:

1
2
3
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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
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:

1
go run main.go arg1 arg2 arg3


The program will then output:

1
2
3
4
5
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.

Facebook Twitter LinkedIn Whatsapp Pocket

Related Posts:

To write a simple "Hello World" program in Haskell, follow these steps:Open a text editor or an Integrated Development Environment (IDE) that supports Haskell programming. Create a new Haskell source file with a .hs extension, such as helloWorld.hs. In...
In Golang, loops and control structures are essential for controlling the flow of execution in a program. They allow you to iterate through collections of data, perform repetitive tasks, and make decisions based on certain conditions. Here is an overview of ho...
To create a new Golang project, follow these steps:Set up your development environment: Install Golang: Download and install the Go programming language from the official website (https://golang.org). Configure Go workspace: Set up your Go workspace by creatin...