-
Notifications
You must be signed in to change notification settings - Fork 36
/
service.go
50 lines (37 loc) · 969 Bytes
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main
import "errors"
// Service Define a service interface
type Service interface {
// Add calculate a+b
Add(a, b int) int
// Subtract calculate a-b
Subtract(a, b int) int
// Multiply calculate a*b
Multiply(a, b int) int
// Divide calculate a/b
Divide(a, b int) (int, error)
}
//ArithmeticService implement Service interface
type ArithmeticService struct {
}
// Add implement Add method
func (s ArithmeticService) Add(a, b int) int {
return a + b
}
// Subtract implement Subtract method
func (s ArithmeticService) Subtract(a, b int) int {
return a - b
}
// Multiply implement Multiply method
func (s ArithmeticService) Multiply(a, b int) int {
return a * b
}
// Divide implement Divide method
func (s ArithmeticService) Divide(a, b int) (int, error) {
if b == 0 {
return 0, errors.New("the dividend can not be zero!")
}
return a / b, nil
}
// ServiceMiddleware define service middleware
type ServiceMiddleware func(Service) Service