Go Variables

Variables in Go refer to the names associated with values like numbers, or strings. When we declare a go variable, we specify the type and optionally the default value. If we don’t specify the default value, go automatically assigns the default value to a go variable.

Go is a statically-type language that means when we declare a variable, we also specify a data type. From the data type, Go compiler knows what type of value this variable can store. If we try to store a different value, Go compiler will give a compile-time error.

Go Variables Declaration

Go has a special syntax for declaring variables. First we put “var” keyword, then the variable name, and at the end, we put data type like shown below

var variableName dataType

Go also supports alternative variable declarations methods, but we can only use this style within functions. In this syntax, we do not need to specify the type name. Go compiler automatically assigns a data type based on the assigned value. Below is the syntax.

x := 22 
y := 23.22 
z := "string"

GoLang Data Types

Go language supports below data types.

  1. Booleans
  2. Integers
  3. Floats
  4. Complex
  5. Strings

Go Boolean Variable

We can declare boolean variables with a bool type. Boolean variables support only two values: true or false. By default, false is assigned to a boolean variable.

Below is the syntax for defining a boolean variable in GoLang.

var isSuccess bool // by default, false value is assigned 
var isFalse bool = false 
var isTrue = true

When we do not specify the data type during a declaration, the GoLang compiler automatically assigns a data type to a variable based on the assigned value.

Go Integer Variable

GoLang supports 8 different integer variables. It also supports both signed and unsigned data types. A signed data type can store negative values and an unsigned data type can only store non-negative values.

Below is list of integer data types in GoLang.

Type NameRange
int8-128 to 127
int16-32768 to 32767
int32-2147483648 to 2147483647
int64-9223372036854775808 to 9223372036854775807
uint80 to 255
uint160 to 65536
uint320 to 4294967295
uint640 to 18446744073709551615

Go also provides aliases named byte, int, and uint.

  • byte is an alias for uint8.
  • int is an alias for int32 or int64 based on the CPU architecture.
  • uint is an alias for uint32 or uint64 based on the CPU architecture.

Below is the syntax for declaring integer data types in GoLang.

var i int // by default, 0 is assigned 
var m int = 52 
var age = 42

Go Float Variable

Float variables are used to store real numbers. Go supports two floating data types for float variables as shown below:

  1. float32
  2. float64

Below is the syntax for declaring floating variables in GoLang.

var smallNum float32 = 3234234343.443343 
var bigNum float64 = 57483837849887747883748374834.44383478

Go does not support alias for float data types. We have to define explicit float32 or float64.

Go Complex Variable

Supports two complex data types:

  1. complex64
  2. complex128

complex64 uses float32 for real and imaginary parts and complex128 uses float64 for real and imaginary part.

var cnt1 float32 = 1.1 
var cnt2 float32 = 2 

var exp1 = complex(cnt1, cnt2) // complex 64 

var cnt3 float64 = 2.2 
var cnt4 float64 = 3 

var exp2 = complex(cnt3, cnt4) // complex 128

Go String Variable

String data type is used for storing a sequence of characters. By default, an empty string is assigned to the string variable.

Below is the syntax for declaring String data types:

var university string = "III" 
var blankString string // empty 
string var name = "Mark"

Summary

In this tutorial, we learn about Go Language variables and data types. Go supports all necessary variable data types.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.