Skip to content

Instantly share code, notes, and snippets.

@AryanGodara
Last active July 17, 2023 18:17
Show Gist options
  • Save AryanGodara/549b9cbb50acb5ee9fcdb579fd4a2035 to your computer and use it in GitHub Desktop.
Save AryanGodara/549b9cbb50acb5ee9fcdb579fd4a2035 to your computer and use it in GitHub Desktop.
service/service.go
package service
import (
"context"
"github.com/go-kit/log"
)
type service struct {
logger log.Logger
}
// Service interface describes a service that adds numbers
type Service interface {
Add(ctx context.Context, numA, numB float32) (float32, error)
Subtract(ctx context.Context, numA, numB float32) (float32, error)
Multiply(ctx context.Context, numA, numB float32) (float32, error)
Divide(ctx context.Context, numA, numB float32) (float32, error)
}
// NewService creates a new service with all the expected dependencies and returns it
func NewService(logger log.Logger) Service {
return &service{
logger: logger,
}
}
// Add func implements the Service interface
func (s service) Add(ctx context.Context, numA, numB float32) (float32, error) {
return numA + numB, nil
}
// Subtract func implements the Service interface
func (s service) Subtract(ctx context.Context, numA, numB float32) (float32, error) {
return numA - numB, nil
}
// Multiply func implements the Service interface
func (s service) Multiply(ctx context.Context, numA, numB float32) (float32, error) {
return numA * numB, nil
}
// Divide func implements the Service interface
func (s service) Divide(ctx context.Context, numA, numB float32) (float32, error) {
if numB == 0 {
return 0, nil // You can handle error handling in your own way here :)
}
return numA / numB, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment