Tutorial by Examples

In Go, an interface is just a set of methods. We use an interface to specify a behavior of a given object. type Painter interface { Paint() } The implementing type need not declare that it is implementing the interface. It is enough to define methods of the same signature. type Rembrandt ...
In go it can sometimes be useful to know which underlying type you have been passed. This can be done with a type switch. This assumes we have two structs: type Rembrandt struct{} func (r Rembrandt) Paint() {} type Picasso struct{} func (r Picasso) Paint() {} That implement the Painter ...
Interfaces and implementations (types that implement an interface) are "detached". So it is a rightful question how to check at compile-time if a type implements an interface. One way to ask the compiler to check that the type T implements the interface I is by attempting an assignment us...
Type switches can also be used to get a variable that matches the type of the case: func convint(v interface{}) (int,error) { switch u := v.(type) { case int: return u, nil case float64: return int(u), nil case string: return strconv(u) default: ...
You can access the real data type of interface with Type Assertion. interfaceVariable.(DataType) Example of struct MyType which implement interface Subber: package main import ( "fmt" ) type Subber interface { Sub(a, b int) int } type MyType struct { Msg stri...
In mathematics, especially Set Theory, we have a collection of things which is called set and we name those things as elements. We show a set with its name like A, B, C, ... or explicitly with putting its member on brace notation: {a, b, c, d, e}. Suppose we have an arbitrary element x and a set Z, ...

Page 1 of 1