DEV Community

Harendra Kumar Kanojiya
Harendra Kumar Kanojiya

Posted on • Originally published at enlear.academy on

Go Lang Tutorial: Introduction

Introduction to Go Lang, Installation, Hello World program, Variable and Constant

Go Lang Tutorial: Introduction
Designed with Canva.com

Golang, often known as GO, is extremely popular, and rightfully so. It’s not as difficult to learn as C or C++, but it’s still quick , and it has a vibrant community as well as a wealth of useful packages and libraries. The language was created at Google by some of the great minds in computer science.

These are presumably sufficient reasons to investigate the language used to create Docker and Kubernetes. Here’s everything you’ll need to get started with Golang. Have a blast!

Install Golang

Golang is providing the setup for each operating system such as Windows, Mac OS, or Linux. Step by step guide is available on their official website. Click here to read more and install Golang in your system.

Steps to install Golang In windows

Download installer — here

Open the MSI file you downloaded and follow the prompts to install Go.

By default, the installer will install Go to Program Files or Program Files (x86). You can change the location as needed. After installing, you will need to close and reopen any open command prompts so that changes to the environment made by the installer are reflected at the command prompt.

Verify that you’ve installed Go.

  1. In Windows , click the Start menu.
  2. In the menu’s search box, type cmd, then press the Enter key.
  3. In the Command Prompt window that appears, type the following command:
go version
Enter fullscreen mode Exit fullscreen mode
  1. Confirm that the command prints the installed version of Go.

Hello World

I hope you completed the installation process from the previous step. Now it's time to write our first code for “Hello World” in Golang.

To write the first program in Golang we have to create a file with the ‘main.go’ name and copy and paste the following code.

package main

import "fmt"

func main() {
  message := greetMe("world")
  fmt.Println(message)
}

func greetMe(name string) string {
  return "Hello, " + name + "!"
}
Enter fullscreen mode Exit fullscreen mode

Code explanation

In the above code, we have the main package with the main() function which will execute at the file of code execution. The greetMe("world") will return the word by prepending hello .

Variables

In computer programming, a variable or scalar is an abstract storage location paired with an associated symbolic name, which contains some known or unknown quantity of information referred to as a value; or in simpler terms, a variable is a container for a particular set of bits or type of data. Wikipedia

We can declare in two ways

var msg string
msg = "Hello"
Enter fullscreen mode Exit fullscreen mode

In this method, we are defining the variable with var keyword following with it name and datatype.

msg := "Hello"
Enter fullscreen mode Exit fullscreen mode

This is a shortcut method to define a variable in go lang. We just need to define the variable name and its value with := operator.

Constants

In computer programming, a constant is a value that should not be altered by the program during normal execution , i.e., the value is constant. … This is contrasted with a variable, which is an identifier with a value that can be changed during normal execution, i.e., the value is variable. Wikipedia

const Phi = 1.618
Enter fullscreen mode Exit fullscreen mode

Note: Constants can be a character, string, boolean, or numeric value.

Thank you for reading this article, don’t forget to follow me to read more articles like this. You can also share this story with your friends if you find it helpful for others.

_By evolving a Medium partner, you can support me and your other favored writers! _ 👇

Join Medium with my referral link - Harendra Verma

Learn More


Discussion (0)