123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- package main
- import (
- "fmt"
- "strconv"
- "strings"
- )
- type step struct {
- Destinations []string
- Delay int
- }
- type steps []step
- func parseSteps(in string) (result steps) {
- stepsStr := strings.Split(in, ",")
- for _, stepStr := range stepsStr {
- if len(stepStr) == 0 {
- fmt.Printf("An empty step, skipping.")
- continue
- }
- stepParts := strings.Split(stepStr, ":")
- delay := 0
- if len(stepParts) >= 2 {
- var err error
- delay, err = strconv.Atoi(stepParts[1])
- if err != nil {
- panic(fmt.Errorf("Got error while parsing delay in string \"%v\": %v", stepStr, err.Error))
- }
- }
- result = append(result, step{
- Destinations: strings.Split(stepParts[0], "&"),
- Delay: delay,
- })
- }
- return
- }
- func main() {
- steps := parseSteps("a&b&c:4,b&c&d:2,a")
- fmt.Printf("%v\n", steps)
- }
|