FAQ
For your case:

1) Define an interface on the set of functions that you want to implement
differently, for example:
type DB interface {
         Begin() (*sql.Tx, error)
         Close()
}
2) Move doit outside of the struct and as a function
func Doit(db DB) {
         tx, _ := db.Begin()
         fmt.Println("doing it")
         tx.Commit()
}
3) Call Doit with prod_db and test_db in main. That's it.
Doit(prod_db)
Doit(test_db)

You need to adopt this kind of style in Go (Define interface and work on
interface). You might need to redesign your infrastructure.

On Tuesday, May 6, 2014 11:34:40 AM UTC-7, Peter Lacey wrote:

My OO brain continues to wrestle with Go's type system. After extensive
reading and testing, I think I understand why the following does not work.
My question is how do I accomplish the desired goal?

In short, I want to have a ProductionDatabase type and a TestDatabase
type. The latter overriding sql.DB.Begin() to do a little test magic. I
currently have something like the following, but no matter how many
variations on this theme I come up with, I cannot get my implementation of
Begin() to be called.

In OO terms, I want: "TestDB isa ProdDB isa sql.DB," so that I can call
Doit() on any of them and reference the additional methods of the current
type. I believe the standard answer is 'use interfaces.' But I am loathe
to create the sql.DB interface definition, which the standard library does
not provide, and, in my case, the sql.Tx interface too. Further, if I were
to to create and implement these interfaces, then Doit() can't be a method,
but only a function, since interfaces don't have methods.

I hope this is clear.

-- Pete

// SAMPLE CODE

package main

import (
"database/sql"
"fmt"
_ "github.com/lib/pq"
)

type ProductionDB struct {
*sql.DB
}

func (prod_db *ProductionDB) Doit() {
tx, _ := prod_db.Begin()
fmt.Println("doing it")
tx.Commit()
}

type TestDB struct {
ProductionDB
MyField string
}

// Never gets called
func (test_db *TestDB) Begin() (*sql.Tx, error) {
fmt.Println("Fake Begin() called")
return test_db.DB.Begin()
}

func main() {
prod_db := new(ProductionDB)
db, _ := sql.Open("postgres", "user=foo password=bar dbname=prod-db
sslmode=disable")
prod_db.DB = db

prod_db.Doit()
prod_db.Close()

test_db := new(TestDB)
db, _ = sql.Open("postgres", "user=foo password=bar dbname=test-db
sslmode=disable")
test_db.DB = db

test_db.Doit()
test_db.Close()
}
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

Discussion Posts

Previous

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 4 of 10 | next ›
Discussion Overview
groupgolang-nuts @
categoriesgo
postedMay 6, '14 at 6:34p
activeMay 6, '14 at 8:18p
posts10
users5
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase