Types in GO and Python

Gegham Jivanyan
5 min readJul 15, 2018

Despite of other properties programming languages are statically typed(eg. GO) and dynamically typed(eg. Python).

Dynamically typed means that variable gets its type only after value assignment, while statically typed means you give type in declaration. which allow you to just declare variables without assigning values.
And it is always messy turn from one type of language into another.

Variables are nothing but reserved memory locations to store values. That is why in dynamically typed languages you always assign value to variable in declaration, so system can know how much memory should be allocated for. Also there is not concept variable apart of object, because everything is object in Python.

# Variable declaration in Python
>>> a = 7 # a is a integer
>>> b = 'xyz' # b is a string
>>> a = b # a become string
// Variable delcaration in GO
var intVar int // empty variable of type int
var anotherIntVar1 = 13 // note that variable type not mentioned
// gets type from assigned value
anotherIntVar2 := 55 // without using keyword var
//
notice that assignment operator is :=

From last to definitions we can see that GO allows to declare variables without mentioning type as in dynamically -typed languages, but unlike them Go variables save their types throughout the life(of course, if you do not do type conversion). So we can say that GO is semi-statically semi-dynamically typed language.

Let’s explore variable types in GO and Python

Integers

Integers are variables which contain numeric values. In Python to say int we understand all kind of numbers — integers, floats and complex numbers, which are built-in types in Python). In Go integers, floats and complex are different types and have many representations. Integer has 13 representations — int, int8, int16, int 32, int64, uint, uint8, uint16, uint32, uint64, uintptr, byte(alias for int32) and rune(alias for int32). Float and complex numbers have 2 representations — float32, float64 and complex64, complex128 appropriately. All representations have fixed sizes — int and uint have 64 bit(or 8 byte) sizes, all other representations have sizes according to their names(e.g size(int32) is 32 bit(or 4byte)). So the biggest value we can store in integer(uint64) is 0xFFFFFFFFFFFFFFFF or 2**64–1, which means that we can not work with big numbers. In Python variables have not fixed size, and empty int variable size is 24 byte(note that it is not size of stored value, but size of object of int class, and in 24 byte it can store values less then 2**64). Size can grow according to assigned value, and you can easily work with numbers as big as you want.

>>> import sys
>>> empty_int_var = int()
>>> sys.getsizeof(empty-int_var)
24
>>> int_var= 124556
>>> sys.getsizeof(int_var)
24
>>> big_int_var = 1267650600228229401496703205375
>>> sys.getsizeof(big_int_var)
40

Bools

Bool is the most simple data type in all languages, and it is logical type, and has 2 values True and False.

Go doesn’t have a quick way to evaluate if something is “truthy”. In Python, for example, you can use an if statement on any type and most types have a way of automatically converting to True or False

>>> i = 7
>>> if i:
... # Do something

This is not possible in Go. You really need to do it explicitly for every type:

x := 1
if x != 0 {
// Do something
}

Strings

Strings in both languages(and basically in all programming language I know) are immutable text values representing characters. Default size of string in Python is 37 byte, and growing with every character by 1 byte.

>>> s = ''
>>> sys.getsizeof(s)
37
>>> s = 'a'
>>> sys.getsizeof(s)
38

In Go string variable size is 16 bytes, and never grow, because strVar or anotherStrVar contain only address and size of value you assigned to them, but not a value itself.

var strVar = ''
fmt.Println(unsafe.Sizeof(strVar))
16
var anotherStrVar = 'My string as long as I want'
fmt.Println(unsafe.Sizeof(anotherStrVar))
16

In both languages you can work with strings by indecis. Also there is an huge bunch of string formatting ways and methods.

Arrays and Slices

Go arrays are containers for storing sequenced values of the same type, that are numerically indexed. And if you give array size you cant change it. There is also slice type , which is similar to array, but do not need to have a fixed size. But all elements in both arrays and slices should be the same type.

var myArray [4]float
var anotherArray [7]float
myArray = anotherArray // get an error, because trying to change
// size of myArray
$> cannot use anotherArray(type [7]float) as type [4]float in assignment

In Python analog of array and slice is list. But it is more flexible, no fixed length, can have elements of different types.Map

Go Map is key-value data structure as Python dict, but again in Go keys and values should have the same type.

var myMap map[int]string // myMap has int type keys
// and string type values
// Note: maps,like pointers and slices
// are reference types, so the value of
// myMap is nil here.

To initialize a myMap, use the built in make function:

m = make(map[string]int) // The make function allocates and   
// initializes a hash map data structure
// and returns a map value that points to
// it.
m[0] = "C++"
m[1] = "Go"
m[7] = "Python"
myStr := m[7] // myStr is a string variable with m[7] value.

Struct

A struct is a composite type that stores zero or more elements indexed by a named identifier known as a field. Elements can be variables, other structs, functions. This is analog of Python classes, but python classes have some extra properties like inheritance, polymorphism etc.

// declaration
type person struct {
name string
age int
}
// initialization
person{"Bob", 20} // {Bob 20}
person{name: "Alice", age: 30} // {Alice 30}
person{name: "Fred"} // {Fred 0} - we did not give value to age
// it takes default value of int, which is 0
s := person{name: "Sean", age: 50} // {Sean 50}
p := person{} // { 0}

Interface{}

An interface is two things:

  • it is a set of methods,
  • but it is also a type

The interface{} type, the empty interface is the interface that has no methods.

Since there is no implements keyword, all types implement at least zero methods, and satisfying an interface is done automatically, all types satisfy the empty interface.

That means that if you write a function that takes an interface{} value as a parameter, you can supply that function with any value

CONST

Constancy is one of important and used properties of any programming languages. It is a good style and security to have variables as const when you can do it. Go has a keyword const instead of python. But in python there is a data type called tuple which is immutable, you can not assign twice, but can append new values.

--

--

Gegham Jivanyan

I'm mathematician and programmer. Currently working at Skycryptor startup, which focuses on data encryption. Chess lover and bicyclist.