go面向接口编程

Go 语言是一种静态类型、编译型的编程语言,它以其简洁、高效和并发支持而闻名。Go 语言中的接口(Interface)是一种非常强大的特性,它允许我们以一种类型安全的方式编写灵活的代码。面向接口编程是一种编程范式,它强调使用接口而不是具体实现来设计和构建软件系统。
以下是一些面向接口编程的关键概念和实践:
1. 定义接口:接口是一组方法签名的集合,它定义了一组行为,但不需要实现这些行为。在 Go 中,接口是隐式实现的,这意味着如果一个类型提供了接口中定义的所有方法,那么它就实现了该接口。
type MyInterface interface {
    DoSomething()
    DoAnotherThing()
}
2. 实现接口:任何类型只要实现了接口中定义的所有方法,就自动实现了该接口。
type MyStruct struct {
    // 结构体字段
}

func (m *MyStruct) DoSomething() {
    // 实现方法
}

func (m *MyStruct) DoAnotherThing() {
    // 实现方法
}
3. 使用接口变量:你可以使用接口变量来存储实现了特定接口的任何类型的值。
var myInterface MyInterface
myInterface = &MyStruct{}
myInterface.DoSomething()
4. 多态:接口允许你编写多态代码,即同一个接口可以用于不同的类型。
type AnotherStruct struct {
    // 另一个结构体
}

func (a *AnotherStruct) DoSomething() {
    // 另一个实现
}

func (a *AnotherStruct) DoAnotherThing() {
    // 另一个实现
}

// 可以使用同一个接口变量来引用不同类型的实例
anotherInterface := &AnotherStruct{}
anotherInterface.DoSomething()
5. 依赖注入:接口可以用来实现依赖注入,这是一种减少代码耦合性的技术。
func NewService(dependency MyInterface) *Service {
    return &Service{
        dependency: dependency,
    }
}
6. 接口作为函数参数:你可以将接口作为函数参数,这样函数就可以接受实现了该接口的任何类型的实例。
func DoSomethingWithInterface(i MyInterface) {
    i.DoSomething()
}
7. 接口的空值检查:在 Go 中,接口变量可以持有一个具体的值或者是一个 nil 值。在使用接口变量之前,应该检查它是否为 nil。
if myInterface != nil {
    myInterface.DoSomething()
}
面向接口编程可以帮助你编写更灵活、更易于测试和维护的代码。通过定义清晰的接口,你可以更容易地替换组件的实现,同时也能够编写出更通用的代码。

    1. 面向接口的编程
      1. 定义结构体

type MetadataFactroy struct {
    *factrydb.FacatroyDb

    iface.IFactroyDb  //接口
    cfg *ichubconfig.IchubConfig `json:"-"`
}

func Defult() *MetadataFactroy {

    var metaFactroy = &MetadataFactroy{
       FacatroyDb: factrydb.NewFacatroyDb(),
       cfg:        ichubconfig.FindBeanIchubConfig(),
    }
    metaFactroy.DbClientDto = dbpkgdto.Cfg.ReadIchubDb()
    return metaFactroy.Init()

}

      1. 初始化接口

func (this *MetadataFactroy) Init() *MetadataFactroy {

    if this.IFactroyDb == nil {
       if this.IfMysql() {
          this.IFactroyDb = mysql.NewFactroyMysql()
       }
       if this.IfCockdb() {
          this.IFactroyDb = cockdb.NewFactroyCockRoarch()
       }
       if this.IfPostgres() {
          this.IFactroyDb = postgres.NewFactroyPostgres()
       }
       if this.IfMedusa() {
          this.IFactroyDb = postgres.NewFactroyPostgres()
       }
    }
    this.IFactroyDb.SetImetaCtx(this)
    this.IFactroyDb.SetDbClientDto(this.DbClientDto)
    this.IFactroyDb.SetTableName(this.TableName)

    return this
}

func (this *MetadataFactroy) FindIFactroyDb() iface.IFactroyDb {
    return this.Init().IFactroyDb
}

      1. 接口使用

func (this *MetadataFactroy) findMetaTableDto() *dbpkgdto.MetaTableDto {
    var idbf = this.FindIFactroyDb()

    var metaTable = dbpkgdto.NewMetaTableDto()
    metaTable.Columns, _ = idbf.FindCols()
    metaTable.TableSchema = this.DbClientDto.Dbname
    metaTable.TableName = this.TableName
    metaTable.TableExist = len(metaTable.Columns) > 0
    this.BuildGoFields(metaTable)
    metaTable.PkInfo = dbpkgdto.NewMetaPkInfo()
    metaTable.PkInfo.TypeName = idbf.FindFactryDto().PkeyType
    metaTable.PkInfo.PkName = idbf.FindFactryDto().Pkey
    metaTable.PkInfo.ColName = stringutils.Camel2Case(metaTable.PkInfo.PkName)

    return metaTable
}

      1. 接口定义

type IFactroyDb interface {
    IniDb(conn string) (dbinst *gorm.DB)
    SetImetaCtx(imetaCtx ImetaCtx)
    ImetaCtx() ImetaCtx

    String() string
    ToString() string

    BuildModel() *dto2.ModeFactors
    MakeModelProto() *list.List
    MakeModelProtoBody(columns *[]dto2.MetaColDto) *list.List

    FindPgPkey(table string) []dto2.MetaPkInfo

    FindColumns() (*[]dto2.MetaColDto, error)
    FindCols() ([]dto2.MetaColDto, error)

    FindTableComment()
    FindTables() []dto2.MetaTableDto
    FindFactryDto() *dto2.FacatroyDto
    SetDbClientDto(dto *baseconfig.DbClientDto)
    SetTableName(tablename string)
    FindMetadata(table string) *dto2.MetaTableDto
    FindFields(table string, fields string) string
}

  • 8
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Golang 是一门支持面向接口编程的语言,这意味着在 Golang 中,我们可以通过定义接口来实现多态性。具体来说,我们可以定义一个接口类型,然后让多个不同的类型实现该接口,从而使它们可以被视为同一类型,从而实现多态性。 在 Golang 中,接口是一组方法的集合,这些方法定义了该接口所代表的对象应该具有的行为。任何类型只要实现了该接口中定义的所有方法,就可以被视为该接口类型的实例。 下面是一个简单的例子,展示了如何定义一个接口和实现该接口: ```go package main import "fmt" // 定义一个接口 type Animal interface { Speak() string } // 定义一个结构体类型 type Dog struct { Name string } // 实现 Animal 接口中的 Speak 方法 func (d Dog) Speak() string { return "Woof!" } func main() { // 创建一个 Dog 类型的实例 d := Dog{Name: "Fido"} // 将该实例赋值给 Animal 类型的变量 var a Animal = d // 调用 Animal 接口中的 Speak 方法 fmt.Println(a.Speak()) // 输出: Woof! } ``` 在上面的例子中,我们定义了一个 Animal 接口,它只有一个方法:Speak。然后我们定义了一个 Dog 结构体类型,并实现了 Animal 接口中的 Speak 方法。最后,我们创建了一个 Dog 类型的实例,并将其赋值给 Animal 类型的变量。由于 Dog 类型实现了 Animal 接口中的所有方法,所以它可以被视为 Animal 类型的实例,从而实现了多态性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

leijmdas

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值