DEV Community

Cover image for Getting Started with Go Programming Language
Luqman Shaban
Luqman Shaban

Posted on

Getting Started with Go Programming Language

Introduction

Go, a statically typed, high-level programming language developed by Google in 2009, is considered one of the best choices for building back-end applications. This includes web development (APIs), cloud services (like Docker), and many other use cases. Go’s speed also makes it well-suited for complex applications.

In this blog, I will walk you through the process of initializing a Go project and printing out a simple Hello, World statement. To follow along, make sure you have Go installed in your machine. Let’s start of by creating a simple hello world project.

Open your terminal, create a new folder named helloWorld and initialize Go inside the project.

mkdir helloWorld
cd helloWorld
go mod init helloWorld

#output
#go: creating new go.mod: module helloWorld
Enter fullscreen mode Exit fullscreen mode
  1. Let’s create a simple program that prints hello world. — Open the project in your preferred code editor — create a main.go file — Paste the following code inside the file
package main

import (
        "fmt"
)

func main() {
        fmt.Println("Hello, World!")
        //prints Hello, World
}
Enter fullscreen mode Exit fullscreen mode

Explanation

1. package main:
This line declares the package name for this code file. In Go, code is organized into packages, which act like folders to group related functionality.
The name main is special and signifies that this package contains the entry point for the program. This means the code within this package will be executed when you run the program.

2. import (“fmt”)

This line imports a package called fmt. Go has a standard library with various pre-written functionalities. The fmt package provides functions for formatted printing and input/output operations.
By importing fmt, you gain access to the functions defined within that package. In this case, we'll be using a function from fmt later in the code.

3. func main() {}

This line defines a function named main. Functions are reusable blocks of code that perform specific tasks.
The main function has a special role in Go programs. It's the starting point where the program's execution begins.
The curly braces {} mark the beginning and end of the function body, where you place the code that the function will execute.

4. fmt.Println(“Hello, World!”)

This line is where the actual action happens. It calls a function named Println from the imported fmt package.
Println takes one argument, which in this case is a string literal "Hello, World!". A string literal is text enclosed in double quotes.
The Println function prints the provided string ("Hello, World!") to the console, followed by a newline character.

Conclusion

Congratulations! You’ve successfully created your first Go program and printed “Hello, World!” to the console. This provides a solid foundation for exploring Go’s functionalities further. In the next section, we’ll delve deeper into Go’s basic building blocks, like variables, data types, and control flow statements. By understanding these fundamentals, you’ll be well-equipped to write more complex Go programs.

Top comments (0)