Port and Adapter in Go with Uber FX
Perm Chao

Perm Chao @mossnana

About: Hello, I'm practicing to be good software engineer.

Location:
Thailand
Joined:
Dec 12, 2019

Port and Adapter in Go with Uber FX

Publish Date: May 8
1 0

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

  • IEmailPort will be interface for adapter

Create core business

File: internal/services/sender/sender.go
EmailSenderService

  • 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
Adapter

  • 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
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

Comments 0 total

    Add comment