繁体   English   中英

使用带有 golang 和数据库抽象的接口

[英]Using interfaces with golang and database abstraction

我想弄清楚如何在 Go 中构建数据存储抽象。 我想我了解接口的基础知识。 但是,我遇到的问题是,网上的所有示例都只向您展示了最简单的案例,除此之外别无他物。

我想要做的是弄清楚如何以及在哪里放置 SQL 代码。 我试图编写最简单的代码来说明我想要做什么(是的,没有错误代码,是的,路径结构不是惯用的)。 我有一个包含两个表的数据库。 一个用于存储圆形,一个用于存储正方形。 我在 Go 中有对象来制作这些。 我的目录结构是:

project/main.go
project/test.db
project/shapes/shape.go
project/shapes/circle/circle.go
project/shapes/square/square.go
project/datastore/datastore.go
project/datastore/sqlite3/sqlite3.go

我能想到的唯一方法是将 SQL INSERT 和 SELECT 代码放在实际的形状文件(circle.go 和 square.go)中。 但这感觉真的不对。 看起来它应该是 datastore/sqlite3 包的一部分。 我只是不知道如何使它起作用。 这是我到目前为止所拥有的:

main.go

package main

import (
    "fmt"
    "project/datastore/sqlite3"
    "project/shapes/circle"
    "project/shapes/square"
)

func main() {
    c := circle.New(4)
    area := c.Area()
    fmt.Println("Area: ", area)

    s := square.New(12)
    area2 := s.Area()
    fmt.Println("Area: ", area2)

    db := sqlite3.New("test.db")
    db.Put(c)
    db.Close()
}

形状/shape.go

package shapes

type Shaper interface {
    Area() float64
}

形状/圆圈/circle.go

package circle

import (
    "project/shapes"
)

type CircleType struct {
    Radius float64
}

func New(radius float64) shapes.Shaper {
    var c CircleType
    c.Radius = radius
    return &c
}

func (c *CircleType) Area() float64 {
    area := 3.1415 * c.Radius * c.Radius
    return area
}

形状/正方形/square.go

package square

import (
    "project/shapes"
)

type SquareType struct {
    Side float64
}

func New(side float64) shapes.Shaper {
    var s SquareType
    s.Side = side
    return &s
}

func (s *SquareType) Area() float64 {
    area := s.Side * s.Side
    return area
}

数据存储/数据存储.go

package datastore

import (
    "project/shapes"
)

type ShapeStorer interface {
    Put(shape shapes.Shaper)
    Close()
}

数据存储/sqlite3/sqlite3.go

package sqlite3

import (
    "database/sql"
    _ "github.com/mattn/go-sqlite3"
    "log"
    "project/datastore"
    "project/shapes"
)

type Sqlite3DatastoreType struct {
    DB *sql.DB
}

func New(filename string) datastore.ShapeStorer {
    var ds Sqlite3DatastoreType

    db, sqlerr := sql.Open("sqlite3", filename)
    if sqlerr != nil {
        log.Fatalln("Unable to open file due to error: ", sqlerr)
    }
    ds.DB = db

    return &ds
}

func (ds *Sqlite3DatastoreType) Close() {
    err := ds.DB.Close()
    if err != nil {
        log.Fatalln(err)
    }
}

func (ds *Sqlite3DatastoreType) Put(shape shapes.Shaper) {
    log.Println("Help")
    // here you could either do a switch statement on the object type
    // or you could do something like shape.Write(), if Write() was defined
    // on the interface of shape/shapes.go Shaper interface and then
    // implemented in the Square and Circle objects. 
}

由于 Circle 和 Square 对象的数据库表会有所不同,因此我需要为每个对象设置一个方法。 如果我向 circle 包和 square 包添加一个方法来进行插入,我可以让它工作。 但就像我上面说的那样,感觉就像我做错了。

非常感谢。

类型切换是正确的做法。 您的逻辑代码(您编写的代码是因为之前没有其他人这样做过)应该对存储一无所知。

假设您想为请求添加一个计数器,并且您决定对 Redis 上的请求进行计数。 然后怎样呢? 也将计数器集合名称添加到 Shape 中?

然后,您应该创建一个新的 ShapeStorer 作为装饰器,并在 put 方法中调用 Redis ShapeStorer 和 sqlite ShapeStorer。

对于 no-sql 数据库,您有时根本不关心架构,您只需序列化对象并保存它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM