Skip to content

Instantly share code, notes, and snippets.

Last active Feb 14, 2020
What would you like to do?
package main
import (
type Response struct {
Data interface{} `json:"data,omitempty"`
Status string `json:"status,omitempty"`
Message string `json:"message,omitempty"`
type Book struct {
Name string `json:"name"`
Author string `json:"author"`
type IBookRepo interface {
Get(id string) (Book, error)
type BookRepo struct{}
func (b BookRepo) Get(id string) (Book, error) {
return Book{Name: "Great shit", Author: "Someone on earth"}, nil
type IBookUsecase interface {
Get(id string) (Book, error)
type BookUsecase struct {
Repo IBookRepo
func (b BookUsecase) Get(id string) (Book, error) {
return b.Repo.Get(id)
type IBookHandler interface {
Handler(c echo.Context) error
type BookHandler struct {
Usecase IBookUsecase
func (b BookHandler) Handler(c echo.Context) error {
bookID := c.Param("id")
book, err := b.Usecase.Get(bookID)
if err != nil {
return c.JSON(http.StatusNotFound, &Response{Status: "error", Message: "Book not found"})
return c.JSON(http.StatusOK, &Response{Data: book, Status: "ok"})
type Server struct {
E *echo.Echo
BookHandler IBookHandler
func (s Server) Init() {
// Do route register and apply middleware here
s.E.GET("/", func(c echo.Context) error {
return c.JSON(http.StatusOK, &Response{Status: "ok", Message: "Hello World"})
s.E.GET("/books/:id", s.BookHandler.Handler)
func (s Server) Run() {
func main() {
h := BookHandler{Usecase: BookUsecase{Repo: BookRepo{}}}
e := echo.New()
s := Server{E: e, BookHandler: h}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment