main.go 815 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package main
  2. import (
  3. "fmt"
  4. "strconv"
  5. "strings"
  6. )
  7. type step struct {
  8. Destinations []string
  9. Delay int
  10. }
  11. type steps []step
  12. func parseSteps(in string) (result steps) {
  13. stepsStr := strings.Split(in, ",")
  14. for _, stepStr := range stepsStr {
  15. if len(stepStr) == 0 {
  16. fmt.Printf("An empty step, skipping.")
  17. continue
  18. }
  19. stepParts := strings.Split(stepStr, ":")
  20. delay := 0
  21. if len(stepParts) >= 2 {
  22. var err error
  23. delay, err = strconv.Atoi(stepParts[1])
  24. if err != nil {
  25. panic(fmt.Errorf("Got error while parsing delay in string \"%v\": %v", stepStr, err.Error))
  26. }
  27. }
  28. result = append(result, step{
  29. Destinations: strings.Split(stepParts[0], "&"),
  30. Delay: delay,
  31. })
  32. }
  33. return
  34. }
  35. func main() {
  36. steps := parseSteps("a&b&c:4,b&c&d:2,a")
  37. fmt.Printf("%v\n", steps)
  38. }