A
A
Andrey2019-03-11 23:14:32
go
Andrey, 2019-03-11 23:14:32

How to fill in the structure map correctly?

Given a slice of three keys - "a", "b", "c", which occur in the slice an arbitrary number of times in an arbitrary order, conditionally, []string{"a","c","b","b" }
It is necessary to fill in the map of structures,
type Content { A int, B int, C int}
According to the following principle:
1. The map key will be the key from the slice.
2. if the key is a - fill in A and B, if b - B and C, if c - A and C.
3. fill in the product of {a = 1, b = 2, c = 3} * by the factor that is formed from the numeric key from the slice, to which 1 is added until it becomes a multiple of 2 or 3.
Did it happen to me, normal? :-)

the code
package main

import (
  "fmt"
)

type Content struct {
  A int
  B int
  C int
}

func main() {

  items := make(map[string]Content)

  for j, key := range []string{"a", "b", "c", "c", "b", "a", "b", "a", "c", "b", "c", "a"} {

    item := items[key]
    i := j

    for {
      i += 1

      if 0 == i%2 || 0 == i%3 {

        break
      }
    }

    switch key {

    case "a":
      item.A += 1 * i
      item.B += 2 * i
      break
    case "b":
      item.B += 2 * i
      item.C += 3 * i
      break
    case "c":
      item.A += 1 * i
      item.C += 3 * i
      break
    }

    items[key] = item
  }

  fmt.Println(items)
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question