Learning GO! | Variables and Constants in GO | By Anjal Bam

Learning GO! | Variables and Constants in GO | By Anjal Bam


3 min read

In my previous tutorial, What the heck is GO!! we covered what Go or Golang is, installation and basic hello world in Go.

This tutorial mainly focuses on Variables and Constants in GO.


A Variable is a container that stores some data. Simple as that. Since Go is a statically typed programming language, the type of a variable is to be set on declaration and is not changed throughout the execution.

The variable declaration syntax in GO is:

var identifierName type


var name string
var age int
var isProgrammer bool

NOTE: When a value is defined like shown above, the variable is automatically assigned a zero-value defined for the specific data-type. A datatype is consists of a set of values and operations that can be carried out on the data.

Declaring variables with initialization

We can declare a variable and initialize it on the same line if we know the value it holds beforehand. The syntax:

var myName string = "Anjal"
var myAge int = "22"

Type inferred declaration

This means the variable will infer the type of the variable from the data we provided.

var myName = "anjal"

Declare variables without the var keyword

variable_name := value
myName := "Anjal" // The variable myName is of type string

Multiple variable declaration

Multiple variables can also be declared in a same line as:

var firstName, lastName string = "Anjal", "Bam"
birthMonth, birthDay := 12, 01

Variable Declaration block

The variables in Go can be grouped together in a block for better readability and better code quality.

var (
  name = "Anjal"
  age = 22
  lovesGo = true

Note: If the values are not initialized to the declared value, Go will automatically set it to the default value known as the zero-value of the type.


The constants are the identifiers with a fixed value that may not be changed.

Declaring constants

package main

import "fmt"

const NAME string = "Anjal"
const AGE = 22

func main() {

Constant declaration block

const (
    PRODUCT  = "Shoes"
    QUANTITY = 1
    PRICE    = 110.25
    STOCK  = true

NOTE: The variables are all uppercase by convention (Although not necessary).

Naming Convention In GO

These are the following rules for naming a Golang variable:

  • A name must begin with a letter, and can have any number of additional letters and numbers.
  • A variable name cannot start with a number.
  • A variable name cannot contain spaces.
  • If the name of a variable begins with a lower-case letter, it can only be accessed within the current package this is considered as unexported variables.
  • If the name of a variable begins with a capital letter, it can be accessed from packages outside the current package one this is considered as exported variables.
  • If a name consists of multiple words, each word after the first should be capitalized like this: empName, EmpAddress, etc.
  • Variable names are case-sensitive (car, Car and CAR are three different variables).


Concluding this post, we covered the variables and constants with different ways we can use to declare these variables.

✌️Like this article? Connect to me on LinkedIn and GitHub.