Port and Adapter in Go with Uber FX
Case Sending email Folder Structure cmd/sender/main.go internal/services/sender/sender.go adapters/gmail/gmail.go adapters/hotmail/hotmail.go Steps Create port File: internal/services/sender/sender.go IEmailPort will be interface for adapter Create core business File: internal/services/sender/sender.go IEmailService will be interface for UberFX EmailService will be struct of service with attribute emailPort(will be injected by UberFX) NewEmailService is constructor Create adapters File: adapters/gmail/gmail.go GmailAdapter will be interface for UberFX File: adapters/hotmail/hotmail.go ** Hotmail like gmail, just replace gmail with hotmail ** Create program File: cmd/sender/main.go NewEmailAdapter function will let UberFX know that IEmailPort interface will use Gmail adapter struct We can switch adapter to hotmail by comment out return hotmail.NewHotmailAdapter() in function NewEmailAdapter

Case
Sending email
Folder Structure
- cmd/sender/main.go
- internal/services/sender/sender.go
- adapters/gmail/gmail.go
- adapters/hotmail/hotmail.go
Steps
Create port
File: internal/services/sender/sender.go
-
IEmailPort
will be interface for adapter
Create core business
File: internal/services/sender/sender.go
-
IEmailService
will be interface for UberFX -
EmailService
will be struct of service with attribute emailPort(will be injected by UberFX) -
NewEmailService
is constructor
Create adapters
-
GmailAdapter
will be interface for UberFX
File: adapters/hotmail/hotmail.go
** Hotmail like gmail, just replace gmail with hotmail **
Create program
-
NewEmailAdapter
function will let UberFX know thatIEmailPort
interface will use Gmail adapter struct - We can switch adapter to hotmail by comment out
return hotmail.NewHotmailAdapter()
in functionNewEmailAdapter