dsoihsnz85757 2013-04-21 17:03
浏览 114
已采纳

Golang:在Go的组合模型中,是否可以通过“父”结构的方法访问“子”结构?

I want to make a generic model struct to embed in structs that will use gorp (https://github.com/coopernurse/gorp) to persist objects in my MySQL database. It's my understanding that this kind of composition is how one accomplishes things in Go that are done with inheritance in strongly OO languages.

I haven't been having good luck, however, as I want to define all of the CRUD methods on the GorpModel struct, to avoid repeating them in each model, but this causes gorp (as I'm using it right now) to assume that the table I want to interact with is called GorpModel due to the reflection that gorp uses. Which causes errors, naturally, as I have no such table in my database.

Is there any way to figure out / use which type I'm in (the superclass which GorpModel is embedded in) to make the below code work, or am I barking up the wrong tree altogether?

package models

import (
    "fmt"
    "reflect"
    "github.com/coopernurse/gorp"
    "database/sql"
    _ "github.com/go-sql-driver/mysql"
)

type GorpModel struct {
    New            bool   `db:"-"`
}

var dbm *gorp.DbMap = nil

func (gm *GorpModel) DbInit() {
    gm.New = true
    if dbm == nil {
        db, err := sql.Open("mysql", "username:password@my_db")
        if err != nil {
            panic(err)
        } 
        dbm = &gorp.DbMap{Db: db, Dialect: gorp.MySQLDialect{"InnoDB", "UTF8"}}
        dbm.AddTable(User{}).SetKeys(true, "Id")
        dbm.CreateTables()
    }           
}

func (gm *GorpModel) Create() {
    err := dbm.Insert(gm)
    if err != nil {
        panic(err)
    }
}

func (gm *GorpModel) Delete() int64 {
    nrows, err := dbm.Delete(gm)
    if err != nil {
        panic(err)
    }

    return nrows
}   

func (gm *GorpModel) Update() {
    _, err := dbm.Update(gm)
    if err != nil {
        panic(err)
    } 
}   

The New property of the GorpModel struct is used to keep track of whether it is a newly created model, and whether we should call Update or Insert on Save (which is defined in the child User struct at the moment).

  • 写回答

1条回答 默认 最新

  • dongtan9253 2013-04-21 17:28
    关注

    Is there any way to figure out / use which type I'm in (the superclass which GorpModel is embedded in)

    No.

    I don't know the best way to structure your solution, but with respect to the CRUD you're trying to implement in some kind of base class, just write them as functions. ie.

    func Create(gm interface{}) { // or whatever the signature should be
        err := dbm.Insert(gm)
        if err != nil {
            panic(err)
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀