Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of a dynamic type of some value in go?

Tags:

types

go

Considering the fact that go is statically typed language, What is the meaning of a dynamic type of some value ?

like image 641
Salah Eddine Taouririt Avatar asked Dec 11 '13 11:12

Salah Eddine Taouririt


1 Answers

The 'dynamic type' of a variable is important when handling interface values. Dynamic types are defined as follows (source):

The static type (or just type) of a variable is the type defined by its declaration. Variables of interface type also have a distinct dynamic type, which is the actual type of the value stored in the variable at run time. The dynamic type may vary during execution but is always assignable to the static type of the interface variable. For non-interface types, the dynamic type is always the static type.

Consider this example:

var someValue interface{} = 2

The static type of someValue is interface{} but the dynamic type is int and may very well change in the future. Example:

var someValue interface{} = 2

someValue = "foo"

In the example above the dynamic type of someValue changed from int to string.

like image 118
nemo Avatar answered Oct 18 '22 18:10

nemo