Data types

Data types define the type of data that a variable can hold. Golang is a statically typed language meaning that a variable needs to define the type of value it will store and once defined, the variable can not store value of any other type.
Golang support many different data types, all of which are explained in this section.
[the_ad id=”651″] bool
bool data type in Golang represents a boolean value. A boolean type can have only 2 values: true or false. A variable of boolean type is declared using bool keyword. Example,

package main

import "fmt"

func main() {
   var flag bool
   fmt.Printf("Type of variable is %T", flag)
}

Above program will print

Type of variable is bool

You can also explicitly assign true or false to a bool variable as var flag bool = true. Default value of a bool variable is false, meaning, if you don’t assign a value to variable, it will be initialized to false.
[the_ad id=”656″] string
A string is a sequence of one or more characters enclosed between double quotes. A string data type is declared using keyword string. Example,

var string s = "codippa"

Remember that you need to use double quotes to define a string in Golang, you cannot use both single or double quotes as in some programming languages such as python or javascript.
Numeric types
All numbers fall under the category of numeric types but there are sub-types of this category which are classified according to the type of numbers. They are
Integer
This data type is used to represent whole numbers or values without decimal or fraction. Examples of this data type are summarized in the table below.

Keyword Description Example
uint8 Used to store positive integers from 0 to 255.
Unsigned means only positive integers.
var num uint8 = 10
uint16 Used to store integers from 0 to 65535.
Unsigned means only positive integers.
var num uint16 = 10000
uint32 Used to store integers from 0 to 4294967295, which is 2^32.
Unsigned means only positive integers.
var num uint32 = 3000000000
uint64 Used to store integers from 0 to 18446744073709551615, which is 2^64.
Unsigned means only positive integers.
var num uint64 = 50000000000
int8 Signed equivalent of uint8.
Can store both negative and positive values in range -128 to 127.
var num int8 = -105
int16 Same as above with range between -32768 and 32767 var num int16 = 25000
int32 Same as above with range between -2147483648 and 2147483647 var num unint32 = 1232312323
int64 Same as above with range between -9223372036854775808 and 9223372036854775807 var num uint64 = 233243434342323

[the_ad id=”3528″] Floating point numbers
Used for decimal values such as 12.56, 99.98723 etc. There are only two variants of this data type depending on the system architecture as

Keyword Description Example
float32 Used to store floating point or decimal values.
Range of this type is -3.4E+38 to 3.4E+38.
var float32 num = 1000000000.000
float64 This data type is same as float32 but with a much larger range of -1.7E+308 to 1.7E+308. var float64 num = 2222222222222222.88
complex32 Complex numbers with real and imaginary parts whose range is the same as that of float32. var num complex32 = 2 + 3i
complex64 Same as complex32 but with a higher range of float64. var num complex64 = 2.232 + 5.54i


Other numeric types

There are other data types in Golang which are variants of the types that have been already discussed above. They are.

Keyword Description Example
byte Same as uint8. If you declare a variable of this type and print its type, it will be uint8. var byte = 125
rune Same as uint32. var rune = 123432565
uint Either 32 or 64 bit integer(that is, same as uint32 or uint64) depending on the system architecture. var uint = 123432565
int Same as uint var int = 123432565

Note that even though uint and uint32 or uint64 may be same on a machine but you cannot assign a variable of type uint to a variable of type uint32 or uint64 and vice-versa. Doing so will raise an error. Example,

var x int = 125
var y uint = x

Above code sample will result in an error.

cannot use x (type int) as type uint in assignment

An explicit cast is required to use uint, int, uint32 and uint64 interchangeably.
[the_ad id=”94″] Other Types
Other data types include
Arrays
Structures
Functions
Pointers
Interfaces
Maps
Channels
You will learn about them in great detail in later sections.

Data Types

Data types refer to the type of data that can be stored in a variable.
Till now, we learnt how to assign values to variables but there are different types of values that can be assigned to a variable.
These different kinds of values are referred to as data type.
[the_ad id=”3385″] Javascript data types fall into following two broad categories.
1. Primitive types
 These types deal with a single value.

Primitive values can be any of the following types.
A. String
A sequence of characters enclosed between single quotes or double quotes is a String.
A string may also be of more than one words separated by a space.
Examples of string are ‘codippa’, ‘javascript’,  ‘learning javascript is fun’ etc.

B. Number

All numbers whether they are whole numbers of decimal number fall into this category of data types.
Examples, 23, 45.65, 0.00001 etc.

C. Boolean

Values of boolean data type are either true or false.

D. undefined

When a variable is declared but has not been assigned a value then its value is undefined. Example,

// declare variable
let x;
console.log(x);  // prints undefined

Type of a variable can be determined using typeof operator followed by the name of the variable. Example, typeof x

E. null
A variable is provided a null value to signify that it holds no value.
A null value is also used to reset the value of a variable to no value.

null is different from undefined in that undefined means that the variable was never assigned a value.
Example,

let x;  // x is undefined
x = 10; // x is 10
x = null; // reset the value of x

If you check the type of null and undefined using typeof operator, then it will be object.

[the_ad id=”644″]

2. Reference types
While primitive data types deal with values, reference data type refers to an object. Objects and Arrays are the types that fall under reference data types.

A javascript object is a group of key-value pairs enclosed between curly braces({ and }).
Example, let website = {name: 'google.com', type: 'search engine'}.

A javascript array is a group of values of same or different primitive types enclosed between square brackets([ and ]). Example, [10, ‘javascript’, 23.5].

The type of variable holding a reference data type is object. Example,

// variable holding an object 
let website = {name: 'google.com', type: 'search engine'}
console.log(typeof website);  // prints object
// variable holding an array
let arr = [10, 'javascript', 23.5];
console.log(typeof arr);  // prints object

Dynamic Typing
Javascript is a dynamically typed language.
This means that the values held by a variable can change at run-time.
A variable can hold a string value at one point and a decimal number or an array at some other point.
Example,

// variable with string value
let x = 'codippa';
console.log(typeof x); // prints string
// change its value to decimal
x = 20.5;
console.log(typeof x); // prints number

 

Data Types

Data types signify the type of data that python can deal with or different types of data that python variables can hold. Python has following standard data types.
1. Numbers
This data type represents numeric values and can be classified into following sub-types
A. int: This type refers to integer or whole numbers which are not fractions or which do not have a decimal such as 0, 28, -10 etc.

B. long: Whole numbers which are large and do not fit in the range of an integer should be stored as long. Octal and hexadecimal numbers are also stored as long. For differentiating between int and long data types, append an ‘l’ or ‘L’ to the number, Examples,

C. float: This data type refers to decimal numbers. Examples are 10.2, 11.0, 99.9234, -56.73 etc. These are also referred to as floating point numbers.

[the_ad id=”89″]

2. Strings
A sequence of characters is called a string in python. A string may be of one word or multiple words separated by a space. It may also span multiple lines. A string is always enclosed between single quotes(‘ and ‘) or double quotes(” and “). Examples are ‘Python’, “I am learning to code”, etc.
There is much more to learn about strings which is available here.

Python version 3.6 does not support a trailing ‘l’ or ‘L’.

3. Lists
A list is a collection of items that may or may not be of same type. That is, a list can contain a string, an int, a float or it may be completely of string values. But often it is used as a collection of similar items. There can be a list of fruits, a list of courses, a list of scores and so on.
For more detailed content on list, redirect to this page.

4. Tuples
A tuple is similar to a list in that it is also a collection of items of same or different data types but it is different from a list as a tuple is immutable. This means that once a tuple is created it cannot be modified. More about tuples here.

5. Dictionaries
A dictionary consists of key-value pairs. Each key and value is separated with a colon(:) while key-value pairs are separated with a comma(,).
Example of a dictionary is {“name”: “Pacific”, “type”: “Ocean”, “name”: “Arabian”, “type”: “Sea”}. It is not mandatory for keys and values to be string. It is also not mandatory for keys and values to be of same type. Dictionaries can be read about in detail here.

Exit mobile version