Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type JsonOut ¶
JsonOut Polymorphism -- concept that you can access objects of different types through the same interface. This is an interface that implements the JsonPrint function for both Regular Humans and Super Humans
type RegularHuman ¶
RegularHuman This is for non-superhuman
func (*RegularHuman) InitRegularHuman ¶
func (r *RegularHuman) InitRegularHuman(first, last, job string, age int)
InitRegularHuman Init a regular human
func (*RegularHuman) JsonPrint ¶
func (r *RegularHuman) JsonPrint() (string, error)
JsonPrint Print out Regularhumans or Superhumans in JSON. This code is attached to an interface so that the same 'function' called JsonPrint can be used with a RegularHuman and SuperHuman type. This saves us having to write some kind of if / then statement tha figures out which type the variable belongs to and printing it out accordingly.
Example ¶
package main import ( "fmt" "github.com/rmasci/oop" ) func main() { var person1 oop.RegularHuman var person2 oop.SuperHuman person1.InitRegularHuman("Peter", "Parker", "Photographer", 23) person2.InitSuperHuman("Spider", "Man", "Spider dude", 23) out, _ := person1.JsonPrint() fmt.Println(out) out, _ = person2.JsonPrint() fmt.Println(out) /* { "First": "Peter", "Last": "Parker", "Age": 23, "Job": "Photographer", "OldPerson": "Your human is young" } { "First": "Spider", "Last": "Man", "Age": 23, "Superpower": "Spider dude", "OldSuperhero": "Super hero is young" } */ }
type SuperHuman ¶
type SuperHuman struct { Human Superpower string OldSuperhero string // contains filtered or unexported fields }
SuperHuman These are fields for Super Humans, it inherits the type and methods of Human
func (*SuperHuman) InitSuperHuman ¶
func (s *SuperHuman) InitSuperHuman(first, last, superpower string, age int)
InitSuperHuman This initializes a superhuman pass in the first last and age
Example ¶
package main import ( "fmt" "github.com/rmasci/oop" ) func main() { var person1 oop.RegularHuman var person2 oop.SuperHuman person1.InitRegularHuman("Peter", "Parker", "Photographer", 23) person2.InitSuperHuman("Spider", "Man", "Spider dude", 23) out, _ := person1.JsonPrint() fmt.Println(out) out, _ = person2.JsonPrint() fmt.Println(out) /* { "First": "Peter", "Last": "Parker", "Age": 23, "Job": "Photographer", "OldPerson": "Your human is young" } { "First": "Spider", "Last": "Man", "Age": 23, "Superpower": "Spider dude", "OldSuperhero": "Super hero is young" } */ }
func (*SuperHuman) JsonPrint ¶
func (s *SuperHuman) JsonPrint() (string, error)
JsonPrint Print out Regularhumans or Superhumans in JSON. This code is attached to an interface so that the same 'function' called JsonPrint can be used with a RegularHuman and SuperHuman type. This saves us having to write some kind of if / then statement tha figures out which type the variable belongs to and printing it out accordingly.