-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathamounts.go
More file actions
38 lines (34 loc) · 895 Bytes
/
amounts.go
File metadata and controls
38 lines (34 loc) · 895 Bytes
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
package main
import (
"errors"
"strconv"
"strings"
)
func getArgumentFromCommand(input string, which int) (output string, err error) {
if len(strings.Split(input, " ")) < which+1 {
return "", errors.New("message doesn't contain enough arguments")
}
output = strings.Split(input, " ")[which]
return output, nil
}
func decodeAmountFromCommand(input string) (amount int, err error) {
if len(strings.Split(input, " ")) < 2 {
errmsg := "message doesn't contain any amount"
// log.Errorln(errmsg)
return 0, errors.New(errmsg)
}
amount, err = getAmount(input)
return amount, err
}
func getAmount(input string) (amount int, err error) {
amount, err = strconv.Atoi(strings.Split(input, " ")[1])
if err != nil {
return 0, err
}
if amount < 1 {
errmsg := "error: Amount must be greater than 0"
// log.Errorln(errmsg)
return 0, errors.New(errmsg)
}
return amount, err
}