Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embed two structs with the same name in a struct

How do you embed two types of the same name in a struct? For instance:

type datastore {
    *sql.Store
    *file.Store
}

Results in duplicate field Store. I know this makes sense, since you wouldn't be able to refer to the embedded field ds.Store but how do you accomplish this anyway?

To clarify, I want to implement an interface with datastore. For that both structs are needed as their methods complement each other to create the interface. What alterantives do I have?

like image 960
Mahoni Avatar asked Jan 31 '17 06:01

Mahoni


1 Answers

Use a type alias, example:

type sqlStore = sql.Store // this is a type alias

type datastore {
    *sqlStore
    *file.Store
}

A type alias doesn’t create a new distinct type different from the type it’s created from. It just introduces an alias name sqlStore, an alternate spelling, for the type denoted by sql.Store.

like image 55
mattes Avatar answered Sep 16 '22 21:09

mattes