Skip to content

Instantly share code, notes, and snippets.

@ricleal
Last active September 17, 2022 07:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ricleal/b8c64852b51709317c0a301c2300ad6a to your computer and use it in GitHub Desktop.
Save ricleal/b8c64852b51709317c0a301c2300ad6a to your computer and use it in GitHub Desktop.
Go Clean Archicture
  • Controller and Presenter are both in a outer layer
classDiagram
class UseCaseInputPort {
  <<interface>>
  Execute()
}
class UseCaseOutputPort {
  <<interface>> 
  PresentHTML()
  PresentTXT()
}
class Presenter {
  PresentHTML()
  PresentTXT()
}
class Controller {
  Call()
}
class UseCaseInteractor {
  Execute()
}
Controller --* UseCaseInputPort: useCaseInputPort
UseCaseInputPort <|-- UseCaseInteractor
UseCaseInteractor --* UseCaseOutputPort: useCaseOutputPort
UseCaseOutputPort <|-- Presenter
package main
import "fmt"
// Reference: https://user-images.githubusercontent.com/20016700/47955442-f0a66c00-dfa0-11e8-83f9-2856ed6ba159.png
// outer
// starts the call
type Controller struct {
useCaseInputPort UseCaseInputPort
}
func (c Controller) Call() {
fmt.Println("Controller.Call() - Get the request")
c.useCaseInputPort.Execute()
fmt.Println("Controller.Call() - Send the response")
}
// show the results
type Presenter struct {
}
func (p Presenter) PresentHTML() {
fmt.Println(" Presenter.PresentHTML() - Show the results")
}
func (p Presenter) PresentTXT() {
fmt.Println(" Presenter.PresentTXT() - Show the results")
}
// inner interfaces
type UseCaseInputPort interface {
Execute()
}
type UseCaseOutputPort interface {
PresentHTML()
PresentTXT()
}
// inner concrete implementation
type UseCaseInteractor struct {
useCaseOutputPort UseCaseOutputPort
}
func (u UseCaseInteractor) Execute() {
fmt.Println(" UseCaseInteractor.Execute() - Business Logic")
u.useCaseOutputPort.PresentHTML()
fmt.Println(" UseCaseInteractor.Execute() - Business Logic cleanup")
}
// Main.go
func main() {
presenter := Presenter{}
interactor := UseCaseInteractor{presenter}
controller := Controller{interactor}
controller.Call()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment