实现Go接口是隐式完成的。在Go中,无需通过指定任何关键字将接口显式实现到具体类型中。若要将接口实现为具体类型,只需为方法提供与接口类型中定义的签名相同的签名即可。

接口类型是用关键字interface定义的。接口定义了一组方法(方法集),但这些方法不包含代码:它们没有实现(它们是抽象的)。方法集是类型必须具有的方法列表,以便实现接口。此外,接口不能包含变量。

 

声明接口:

type catalog interface {
	shipping() float64
	tax() float64
}

实现接口:

以下源代码将可配置类型显示为接口类型目录的实现。可配置类型被定义为具有接收方方法、运输和税费的结构。这一事实自动将可配置限定为目录的实现:

package main
import "fmt"

type catalog interface { 
   shipping() float64 
   tax() float64
} 
 
type configurable struct { 
   name string
   price, qty float64   
} 

func (c *configurable) tax() float64{
  return c.price * c.qty * 0.05
}

func (c *configurable) shipping() float64{
  return c.qty * 5
}

func  main() {
  tshirt := configurable{}
  tshirt.price = 250
  tshirt.qty = 2
  fmt.Println("Shipping Charge: ", tshirt.shipping())
  fmt.Println("Tax: ", tshirt.tax())  
}

输出:

Shipping Charge: 10
Tax: 25

 

使用Go接口进行子类型化:

示例代码:

package main

import "fmt"

type catalog interface { 
   shipping() float64 
   tax() float64
} 
 
type configurable struct { 
   name string
   price, qty float64   
} 

func (c *configurable) tax() float64{
  return c.price * c.qty * 0.05
}

func (c *configurable) shipping() float64{
  return c.qty * 5
}

type download struct{
    name string
    price, qty float64
}

func (d *download) tax() float64{
  return d.price * d.qty * 0.07
}

type simple struct {
  name string
  price, qty float64
}

func (s *simple) tax() float64{
  return s.price * s.qty * 0.03
}

func (s *simple) shipping() float64{
  return s.qty * 3
}

func  main() {
  tshirt := configurable{}
  tshirt.price = 250
  tshirt.qty = 2
  fmt.Println("Configurable Product")
  fmt.Println("Shipping Charge: ", tshirt.shipping())
  fmt.Println("Tax: ", tshirt.tax())  

  mobile := simple{"Samsung S-7",10,25}
  fmt.Println("\nSimple Product")  
  fmt.Println("Shipping Charge: ", mobile.shipping())
  fmt.Println("Tax: ", mobile.tax())  


  book := download{"Python in 24 Hours",19,1}
  fmt.Println("\nDownloadable Product")
  fmt.Println("Tax: ", book.tax())
}

输出:

Configurable Product
Shipping Charge: 10
Tax: 25

Simple Product
Shipping Charge: 75
Tax: 7.5

Downloadable Product
Tax: 1.33

 

实现多个接口给:

在GO中,接口的隐式机制允许同时满足多个接口类型。这可以通过将给定类型的方法集与每个接口类型的方法相交来实现。让我们重新实现前面的代码。已创建新的界面折扣。如下图所示:

package main

import "fmt"

type catalog interface { 
   shipping() float64 
   tax() float64
} 

type discount interface{
    offer() float64
}
 
type configurable struct { 
   name string
   price, qty float64   
} 

func (c *configurable) tax() float64{
  return c.price * c.qty * 0.05
}

func (c *configurable) shipping() float64{
  return c.qty * 5
}

func (c *configurable) offer() float64{
  return c.price * 0.15
}

type download struct{
    name string
    price, qty float64
}

func (d *download) tax() float64{
  return d.price * d.qty * 0.10
}

type simple struct {
  name string
  price, qty float64
}

func (s *simple) tax() float64{
  return s.price * s.qty * 0.03
}

func (s *simple) shipping() float64{
  return s.qty * 3
}

func (s *simple) offer() float64{
  return s.price * 0.10
}

func  main() {
  tshirt := configurable{}
  tshirt.price = 250
  tshirt.qty = 2
  fmt.Println("Configurable Product")
  fmt.Println("Shipping Charge: ", tshirt.shipping())
  fmt.Println("Tax: ", tshirt.tax())  
  fmt.Println("Discount: ", tshirt.offer())  

  mobile := simple{"Samsung S-7",3000,2}
  fmt.Println("\nSimple Product")  
  fmt.Println(mobile.name)
  fmt.Println("Shipping Charge: ", mobile.shipping())
  fmt.Println("Tax: ", mobile.tax())  
  fmt.Println("Discount: ", mobile.offer())

  book := download{"Python in 24 Hours",50,1}
  fmt.Println("\nDownloadable Product")
  fmt.Println(book.name)
  fmt.Println("Tax: ", book.tax())
}

输出:

Configurable Product
Shipping Charge: 10
Tax: 25
Discount: 37.5

Simple Product
Samsung S-7
Shipping Charge: 6
Tax: 180
Discount: 300

Downloadable Product
Python in 24 Hours
Tax: 5

 

接口嵌入:

在GO中,接口类型也支持类型嵌入(类似于struct类型)。这使您可以灵活地以最大化类型重用的方式构建类型。
继续目录示例,声明了一个结构可配置,其中嵌入了类型折扣和礼品包。在这里,您可以创建更具体的目录界面类型。由于giftpack和discount类型是目录接口的实现,因此可配置类型也是目录接口的实现。在类型折扣和礼品包类型中定义的所有字段和方法也可在可配置类型中使用。

示例代码:

package main
import "fmt"

type discount interface{
    offer() float64
}

type giftpack interface{
    available() string
}

type catalog interface { 
   discount
   giftpack
   shipping() float64 
   tax() float64
}

type configurable struct { 
   name string
   price, qty float64   
} 

func (c *configurable) tax() float64{
  return c.price * c.qty * 0.05
}

func (c *configurable) shipping() float64{
  return c.qty * 5
}

func (c *configurable) offer() float64{
  return c.price * 0.15
}

func (c *configurable) available() string{
    if c.price > 1000{
      return "Gift Pack Available"
    }
    return "Gift Pack not Available"
}

type download struct{
    name string
    price, qty float64
}

func (d *download) tax() float64{
  return d.price * d.qty * 0.10
}

func (d *download) available() string{
    if d.price > 500{
      return "Gift Pack Available"
    }
    return "Gift Pack not Available"
}

type simple struct {
  name string
  price, qty float64
}

func (s *simple) tax() float64{
  return s.price * s.qty * 0.03
}

func (s *simple) shipping() float64{
  return s.qty * 3
}

func (s *simple) offer() float64{
  return s.price * 0.10
}

func  main() {
  tshirt := configurable{}
  tshirt.price = 1550
  tshirt.qty = 2
  fmt.Println("Configurable Product")
  fmt.Println("Shipping Charge: ", tshirt.shipping())
  fmt.Println("Tax: ", tshirt.tax())  
  fmt.Println("Discount: ", tshirt.offer())  
  fmt.Println(tshirt.available())

  mobile := simple{"Samsung S-7",3000,2}
  fmt.Println("\nSimple Product")  
  fmt.Println(mobile.name)
  fmt.Println("Shipping Charge: ", mobile.shipping())
  fmt.Println("Tax: ", mobile.tax())  
  fmt.Println("Discount: ", mobile.offer())  

  book := download{"Python in 24 Hours",50,1}
  fmt.Println("\nDownloadable Product")
  fmt.Println(book.name)
  fmt.Println("Tax: ", book.tax())
  fmt.Println(book.available())
}

输出:

Configurable Product
Shipping Charge: 10
Tax: 155
Discount: 232.5
Gift Pack Available

Simple Product
Samsung S-7
Shipping Charge: 6
Tax: 180
Discount: 300

Downloadable Product
Python in 24 Hours
Tax: 5
Gift Pack not Available