Go language entry interface programming

Interface

Definition of interface

  • Interface defines an object-oriented behavior specification

    • Specification only, no implementation
    • Specific objects require specification details
  • practice
    • type defines interface
    • The interface contains a set of method signatures
type Animal interface {
   Talk()
   Eat()
   Run()
}
  • Realization
    • As long as an object contains methods in the interface, it implements the interface
    • An interface type variable can hold instances of any type that implement the interface
type Animal interface {
   Talk()
   Eat()
   Run()
}

type Dog struct {
   name string
}

//As long as an object contains methods in the interface, it implements the interface
func (d *Dog) Eat() {
   fmt.Printf("%s is eating\n",d.name)
}

func (d *Dog) Talk() {
   fmt.Printf("%s is talking\n",d.name)
}

func (d *Dog) Run() {
   fmt.Printf("%s is running\n",d.name)
}

func main() {
   var dog = &Dog{
      name:  "Prosperous wealth",
   }

   var a Animal
   //An interface type variable can hold instances of any type that implement the interface
   a = dog
   a.Eat()
   a.Run()
   a.Talk()
   fmt.Printf("a:%v,dog:%v\n",a,dog)
}

Interface instance

  • Calculate the salaries of all employees in the company
  • The calculation method of each employee is different

Architecture implementation

type Developer struct {
   Name   string
   Base   float64
}

func (d *Developer)Calc() float64{
   return d.Base
}

type PM struct {
   Name  string
   Base  float64
   Option float64
}

func (p *PM)Calc() float64{
   return p.Base + p.Option
}

type YY struct {
   Name string
   Base  float64
   Ratio float64
   Option float64
}

func (y *YY)Calc() float64 {
   return y.Base + y.Option * y.Ratio
}

type EmployeeMgr struct {
   devlist []*Developer
   pmlist []*PM
   yylist []*YY
}

func (e *EmployeeMgr)Calc() float64  {
   var sum float64
   for _,v := range e.devlist {
      sum += v.Calc()
   }

   for _,v1 := range e.pmlist {
      sum += v1.Calc()
   }

   for _,v2 := range e.yylist {
      sum += v2.Calc()
   }

   return sum
}

func (e *EmployeeMgr)AddDev(d *Developer) {
   e.devlist = append(e.devlist, d)
}

func (e *EmployeeMgr)AddPM(p *PM) {
   e.pmlist = append(e.pmlist, p)
}

func (e *EmployeeMgr)AddYY(y *YY) {
   e.yylist = append(e.yylist, y)
}

func main() {
   var e = &EmployeeMgr{}
   dev := &Developer{
      Name: "develop",
      Base: 5000,
   }
   e.AddDev(dev)

   pm := &PM{
      Name:   "pm",
      Base:   10000,
      Option: 2000,
   }
   e.AddPM(pm)

   yy := &YY{
      Name: "yy",
      Base: 23000,
      Option: 9000,
      Ratio: 0.67,
   }
   e.AddYY(yy)

   sum := e.Calc()
   fmt.Printf("sum:%.2f\n",sum)
}

Interface implementation

type Employee interface {
   Calc() float64     //Interface return
}

type Developer struct {
   Name   string
   Base   float64
}

func (d *Developer)Calc() float64{
   return d.Base
}

type PM struct {
   Name  string
   Base  float64
   Option float64
}

func (p *PM)Calc() float64{
   return p.Base + p.Option
}

type YY struct {
   Name string
   Base  float64
   Ratio float64
   Option float64
}

func (y *YY)Calc() float64 {
   return y.Base + y.Option * y.Ratio
}

type EmployeeMgr struct {
   employeelist []Employee
}

func (e *EmployeeMgr) Calc() float64 {
   var sum float64
   for _,v := range e.employeelist {
      sum += v.Calc()
   }
   return sum
}

func (e *EmployeeMgr) AddEmployee(d Employee) {
   e.employeelist = append(e.employeelist,d)
}

func main() {
   var e = &EmployeeMgr{}
   dev := &Developer{
      Name: "develop",
      Base: 5000,
   }
   e.AddEmployee(dev)

   pm := &PM{
      Name:   "pm",
      Base:   10000,
      Option: 2000,
   }
   e.AddEmployee(pm)

   yy := &YY{
      Name: "yy",
      Base: 23000,
      Option: 9000,
      Ratio: 0.67,
   }
   e.AddEmployee(yy)

   sum := e.Calc()
   fmt.Printf("sum:%.2f\n",sum)
}

Empty interface

  • Empty interface has no methods defined
  • So any type implements an empty interface
func main() {
   var a interface{}
   var b int
   a = b
   fmt.Printf("a=%v,a:%T\n",a,a)
   var c float64
   a = c
   fmt.Printf("a=%v,a:%T\n",a,a)
}

Type Asserts

  • Need to introduce V, OK: = I (T) mechanism
func Describe(a Animal) {
   /*Code pit
   dog := a.(*Dog)
   dog.Eat()
    */
   dog,ok := a.(*Dog)
   if !ok {
      fmt.Printf("convert to dog error\n")
      return
   }
   fmt.Printf("describe dog succ\n")
   dog.Run()
   fmt.Printf("describe dog succ------\n")
}
  • Type Asserts
func DescribeSwitch(a Animal) {
   fmt.Printf("DescribeSwitch(a) Begin\n")
   switch a.(type) {
   case *Dog:
      dog := a.(*Dog)
      dog.Run()
   case *Pig:
      pig := a.(*Pig)
      pig.Eat()
   }
   fmt.Printf("DescribeSwitch(a) End\n")
}
  • Improved version of type assertion
func DescribeSwitch(a Animal) {
   fmt.Printf("DescribeSwitch(a) Begin\n")
   switch v:=a.(type) {
   case *Dog:
      dog := v
      dog.Run()
   case *Pig:
      pig := v
      pig.Eat()
   }
   fmt.Printf("DescribeSwitch(a) End\n")
}
  • Pointer reception
  • Multiple interfaces can be implemented for the same type
type Animal interface {
   Talk()
   Eat()
   Run()
}

type BuRuAnimal interface {
   ChiNai()
}

type Dog struct {
   name string
}

//As long as an object contains methods in the interface, it implements the interface
func (d Dog) Eat() {
   fmt.Printf("%s is eating\n",d.name)
}

func (d Dog) Talk() {
   fmt.Printf("%s is talking\n",d.name)
}

func (d Dog) Run() {
   fmt.Printf("%s is running\n",d.name)
}

func (d Dog) ChiNai() {
   fmt.Printf("%s is chinai\n",d.name)
}

func main() {
   var dog = &Dog{
      name:  "Prosperous wealth",
   }
   var a Animal
   //An interface type variable can hold instances of any type that implement the interface
   a = dog
   //Describe(a)
   //DescribeSwitch(a)
   a.Eat()
   var dogVar = Dog{name:"Lai Fu"}
   a = dogVar
   a.Run()
   //Implement multiple interfaces
  var b BuRuAnimal
  b = dog
  b.ChiNai()
}
  • Interface nesting
type Animal interface {
   Talk()
   Eat()
   Run()
}

type BuRuAnimal interface {
   ChiNai()
}

type AdvanceAnimal interface {
   Animal
   BuRuAnimal
}

type Dog struct {
   name string
}

//As long as an object contains methods in the interface, it implements the interface
func (d Dog) Eat() {
   fmt.Printf("%s is eating\n",d.name)
}

func (d Dog) Talk() {
   fmt.Printf("%s is talking\n",d.name)
}

func (d Dog) Run() {
   fmt.Printf("%s is running\n",d.name)
}

func (d Dog) ChiNai() {
   fmt.Printf("%s is chinai\n",d.name)
}

func main() {
   var dog = &Dog{
      name:  "Prosperous wealth",
   }
   var a AdvanceAnimal
   a = dog
   a.ChiNai()
}

Interface instance details

  • writer interface in io package
type Test struct {
   data string
}

func (t *Test)Write(p []byte) (n int,err error) {
   t.data = string(p)
   return len(p),nil
}

func FprintfDemo() {
   file,_ := os.Create("./a.txt")
   fmt.Fprintf(os.Stdout,"hello world\n")
   fmt.Fprintf(file,"hello World\n")   //Terminal file write

   var t *Test = &Test{}
   //Write the string to memory and then read it
   fmt.Fprintf(t,"this is a test interface %s","dasdada")
   fmt.Printf("t.data=%s\n",t.data)

}
  • Stringer interface in fmt package
    Return string
type Student struct {
   Name string
   Age  int
}

func (s *Student)String()string {
   data,_ := json.Marshal(s)
   return string(data)
}

func main() {
   var s = &Student{
      Name: "ABC",
      Age: 22,
   }
   fmt.Printf("s = %v\n",s)
}
  • error interface
type MyError struct {
   When time.Time
   What string
}

func (e *MyError) Error()string{
   str:= fmt.Sprintf("time=%v,message=%s\n",e.When,e.What)
   fmt.Printf("1:%T\n",str)
   return str
}

func run()error {
   fmt.Printf("0\n")
   str:=MyError{time.Now(),"it did not work well"}
   fmt.Printf("2:%T\n",str)
   fmt.Printf("%v,%s\n",time.Now(),"error to run!!")
   return &str
}

func main() {
   if err := run();err!=nil {
      fmt.Printf("3:%T\n",err)
      fmt.Println(err)
   }
}

Tags: Go JSON

Posted on Tue, 14 Apr 2020 08:28:25 -0700 by andrewburgess