Skip to content

Instantly share code, notes, and snippets.

@reetasingh
Created May 2, 2023 06:17
Show Gist options
  • Save reetasingh/06ea8c6d56ddc8a9720a333e24f6f838 to your computer and use it in GitHub Desktop.
Save reetasingh/06ea8c6d56ddc8a9720a333e24f6f838 to your computer and use it in GitHub Desktop.
strong coupling without interfaces example
package main
import "fmt"
type Person struct {
ID int
Name string
}
type PersonDB struct {
// Database connection and operations
}
func (db *PersonDB) Save(person *Person) error {
// Implementation for saving a person to the database
fmt.Printf("Saving person: %+v\n", person)
return nil
}
type PersonService struct {
db *PersonDB
}
func (s *PersonService) CreatePerson(person *Person) error {
// Business logic for creating a person
err := s.db.Save(person)
if err != nil {
return err
}
return nil
}
func main() {
db := &PersonDB{}
service := &PersonService{db: db}
person := &Person{
ID: 101,
Name: "John B",
}
err := service.CreatePerson(person)
if err != nil {
fmt.Println("Error:", err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment