Hello Golang: Writing your first Golang Program

When we start learning a new programming language, we typically start by writing the classic “Hello, World” program.

So Let’s write the “Hello, World” program in Go and understand how it works. Open your favorite text editor, create a new file named hello.go, and type in the following code -

// My first Program
package main

import "fmt"

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

You can run the above program using go run command like so -

$ go run hello.go
Hello, World

The go run command does two things - It first compiles the program to machine code and then runs the compiled code.

If however, you want to produce a standalone binary executable from your Go source that can be run without using the Go tool, then use the go build command -

$ go build hello.go
$ ls
hello		hello.go

You may now run the built binary file like this -

$ ./hello
Hello, World

Understanding the internals of the “Hello, World” program

Let’s go through each line of the “Hello, World” program one by one and understand what it does-

  • Line 1: The first line that starts with // is a comment -

    // My first Program

    Comments are ignored by the Go compiler. They are used to make it easier for others to understand your code.

    Go supports two different styles of comments -

    1. Single-line comment

      // This is a Single line Comment. Everything in this line is ignored by the compiler	
    2. Multi-line comment

      /*
        This is a Multi line Comment.  
        As the name suggests, It can span multiple lines.
      */
  • Line 2: The second line is a package declaration -

    package main

    Every Go program starts with a package declaration. Packages are used to organize related go source code files into a single unit and make them reusable.

    The package “main” is a special go package that is used with programs that are meant to be executable.

    There are two types of programs in Go - Executable Programs and Libraries. Executable Programs are programs that can be run from the command line. Libraries are reusable pieces of code that are used by other programs to perform some task.

  • Line 3: The third line is an import statement -

    import "fmt"

    The import keyword is used to import reusable pieces of code from other packages to use in our program.

    The “fmt” package contains code for dealing with I/O.

  • Line 4: The fourth line is a function declaration -

    func main() {
        // ...
    } 

    A function is a unit of code that contains one or more instructions to perform a task. We typically break a program into smaller functions that take some input, do some processing on the input, and produce an output.

    A function in go is declared using the func keyword.

    The main() function is the entry point of an executable program in Go. It is the first thing that is invoked when you run an executable program.

  • Line 5: This line contains a call to the Println() function of the fmt package -

    fmt.Println("Hello, World")

    We pass the String “Hello, World” to the Println() function which prints it to the standard output along with a new line.

Conclusion

Congratulations! You just wrote your first program in Go. Thank you for reading and making it till the end.

Stay tuned and subscribe to my newsletter to get news about my upcoming tutorials on Golang.

Next Article: Golang Variables, Zero Values, and Type inference

Code Samples: github.com/callicoder/golang-tutorials