Inheritance and Composition in GO
Posted on August 16, 2020
(Last modified on August 27, 2020)
| 2 minutes
| Kinshuk Chandra
Structs can “inherit” from other structs by a method known as embeding. Here is a simple example:
package main import ( "fmt" ) type a struct { Name string } //embeds a value of type a type b struct { a } //embeds a pointer to an a type c struct { *a } func main() { a := a{Name: "Janeway"} fmt.Println(a.Name) b := &b{a: a} fmt.Println(b.Name) c := &c{a: &a} fmt.
[Read More]Interface in Go
Posted on August 16, 2020
(Last modified on August 27, 2020)
| 4 minutes
| Kinshuk Chandra
Intro We declare an Interface much like as we define a user defined type. Interfaces decouple different types from each other so we can create more maintainable programs.
More:
An Interface is a Protocol, a Contract.
Bigger the Interface the weaker the abstraction. –> Rob Pike
It’s an abstract type. It doesn’t have any implementation. It only describes the expected behavior.
The opposite of Abstract Type is Concrete Type.
All the types in Go except Interface are of Concrete Type.
[Read More]