Created
January 24, 2023 11:50
-
-
Save arriqaaq/31c193a38a5bd943fd151b2779dadd0f to your computer and use it in GitHub Desktop.
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 | |
type Repository interface { | |
Find(id int) (interface{}, error) | |
Save(data interface{}) error | |
} | |
type User struct { | |
ID int | |
Name string | |
} | |
type UserRepository struct {} | |
func (r UserRepository) Find(id int) (interface{}, error) { | |
return User{ID: id, Name: "John Doe"}, nil | |
} | |
func (r UserRepository) Save(data interface{}) error { | |
user := data.(User) | |
fmt.Printf("Saving user %d: %s\n", user.ID, user.Name) | |
return nil | |
} | |
type Service struct { | |
repository Repository | |
} | |
func (s Service) FindUser(id int) (interface{}, error) { | |
return s.repository.Find(id) | |
} | |
func (s Service) SaveUser(user User) error { | |
return s.repository.Save(user) | |
} | |
func main() { | |
userRepository := UserRepository{} | |
service := Service{repository: userRepository} | |
user, _ := service.FindUser(1) | |
fmt.Println(user) // User{ID: 1, Name: "John Doe"} | |
service.SaveUser(User{ID: 1, Name: "Jane Smith"}) // Saving user 1: Jane Smith | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment