Salah Eddine Taouririt
Salah Eddine Taouririt

Reputation: 26415

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

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

Upvotes: 16

Views: 13923

Answers (1)

nemo
nemo

Reputation: 57619

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.

Upvotes: 46

Related Questions