This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type DBType interface { | |
LocalDatabase | InMemoryDatabase | |
} | |
type DBPointer[T any] interface { | |
*T | |
PersonDB | |
} | |
func CreatePersonDB[T DBType, dbPointer DBPointer[T]](person Person) error { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func CreatePersonDB[T PersonDB](person Person) error { | |
db := new(T) | |
db.save(person) | |
return nil | |
} | |
func main() { | |
person := Person{ | |
ID: 101, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func CreatePersonDB[T PersonDB](person Person) error { | |
db := new(T) | |
db.save(person) | |
return nil | |
} | |
func main() { | |
person := Person{ | |
ID: 101, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func CreatePersonDB(persondbtype string, person Person) error { | |
var db PersonDB | |
if persondbtype == "local" { | |
db = new(LocalDatabase) | |
} else if persondbtype == "in-memory" { | |
db = new(InMemoryDatabase) | |
} else { | |
return fmt.Errorf("persondbtype not supported :%s", persondbtype) | |
} | |
db.save(person) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// You can edit this code! | |
// Click here and start typing. | |
package main | |
import "fmt" | |
type Person struct { | |
ID int | |
Name string | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"testing" | |
"github.com/stretchr/testify/assert" | |
"github.com/stretchr/testify/mock" | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type mockTestDB1 struct {} | |
type mockTestDB2 struct {} | |
func (m *mockTestDB1) Save(person *Person) error { | |
// case 1 | |
return nil | |
} | |
func (m *mockTestDB2) Save(person *Person) error { | |
// case 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"testing" | |
"github.com/stretchr/testify/assert" | |
) | |
type mockTestDB struct { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
type Person struct { | |
ID int | |
Name string | |
} | |
type PersonDB interface { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
type Person struct { | |
ID int | |
Name string | |
} | |
type PersonDB struct { |
NewerOlder