How to design a state variable and increment based on special conditions?

Viewed 53

Here is my program structure

type myType struct {
   Attr map[string]any
   // some other variables
}
func (t *Tran) foo () {
  // some logic
  count := 0
  for i, v := range myArray {
    count = t.boo(i, v, count)
  }  
}

func (t *Tran) boo (i int, v string, count int) int {
  // bunch of logic
  e := myType{}
  if v == "special" {
    e.Attr = map[string]any{"num": count}
    count += 1
  else {
    e.Attr = map[string]any{}
  }
  t.state[i] = e
  return count
}

Notice the count variable increments based on the "special" string. The program works fine. But I feel there might be smarter way to implement this count state variable. Also Foo can be called many times.

Any advice?

0 Answers
Related