Skip to content

Instantly share code, notes, and snippets.

@dickyaryag6
Created October 23, 2021 12:16
Show Gist options
  • Select an option

  • Save dickyaryag6/110fbcf95cd95362283cd87c1461fe54 to your computer and use it in GitHub Desktop.

Select an option

Save dickyaryag6/110fbcf95cd95362283cd87c1461fe54 to your computer and use it in GitHub Desktop.
package usecase
import (
"errors"
"gomocking/repository"
"strconv"
)
type Usecase struct {
Database repository.DatabaseInterface
}
func New(usecase Usecase) Usecase {
return usecase
}
func (u *Usecase) Addition(number1, number2 string) (int, error) {
number1Int, err := convertStringToInt(number1)
if err != nil {
return 0, err
}
number2Int, err := convertStringToInt(number2)
if err != nil {
return 0, err
}
result := number1Int + number2Int
err = u.Database.Insert(result)
if err != nil {
return 0, err
}
return result, nil
}
func convertStringToInt(s string) (int, error) {
n, err := strconv.Atoi(s)
if err != nil {
return 0, errors.New("Input can't be converted to integer")
}
return n, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment