Tag: interface

json: cannot unmarshal object into Go struct field

Problem json: cannot unmarshal object into Go struct field Code package main import ( “encoding/json” “fmt” ) // DTO object type mould struct { Particle tiny } type tiny interface { GetName() string } // Implement the interface type speck struct { Name string `json:”name”` } func (s *speck) GetName() string { return s.Name } // Constructor func NewSpeck(name string)…

Interface Quirks in Golang

An interface defines a set of methods. A struct type implements these methods and qualifies the object type as an interface type. However, how the struct implements the interface? There are two ways: As a pointer handler As a value handler Implementation with a struct value handler package main import ( “fmt” ) type Dummy interface { Add(int, int)int }…

Golang Essentials for C & C++ Developers: Part III

The Empty Interface An empty interface is used when type of a variable is unknown. There are scenarios such as printf function, raw buffers where you would not know the type. Use case // Definition of a map is –> var map[key]value // If we do not know the type of the value and want // to use the map…