Skip to content

Instantly share code, notes, and snippets.

@eminetto
Created June 26, 2025 15:26
Show Gist options
  • Save eminetto/44ba83c7bfe78b0e874ef2af1b19b10f to your computer and use it in GitHub Desktop.
Save eminetto/44ba83c7bfe78b0e874ef2af1b19b10f to your computer and use it in GitHub Desktop.
main.go
package main
import "context"
type Repository interface {
Func1() error
Func2() error
}
type Repo struct{}
func (r *Repo) Func1() error {
// Implementação da lógica
return nil
}
func (r *Repo) Func2() error {
return nil
}
type UseCase interface {
Save(ctx context.Context, data string) error
Remove(ctx context.Context, id string) error
}
type Service struct {
Repo Repository
}
func (s *Service) Save(ctx context.Context, data string) error {
return nil
}
func (s *Service) Remove(ctx context.Context, id string) error {
return nil
}
func (s *Service) CalculeteSomething(ctx context.Context) (float64, error) {
// Implementação da lógica de cálculo
return 1.0, nil
}
func NewServiceReturningInterface(repo Repository) UseCase {
return &Service{
Repo: repo,
}
}
func NewServiceReturningStruct(repo Repository) *Service {
return &Service{
Repo: repo,
}
}
func main() {
ctx := context.Background()
r := &Repo{}
s1 := NewServiceReturningInterface(r)
_ = s1.Save(ctx, "data")
//s1 só vai ter acesso as funções da interface
s2 := NewServiceReturningStruct(r)
_ = s2.Save(ctx, "data")
_ = s2.Remove(ctx, "id")
_, _ = s2.CalculeteSomething(ctx)
//s2 tem acesso a todas as funções da struct
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment