V
V
Vladimir Grabko2016-08-03 07:34:38
go
Vladimir Grabko, 2016-08-03 07:34:38

How to overcome code duplication?

There are helpers for robots with a global map (it is possible to work with it only through them). Here I did, but the code is duplicated a little less than everywhere. How can this matter be corrected?

func Get(id string) (Sessid, bool) {
  rw := &sync.RWMutex{}
  rw.RLock()
  defer rw.RUnlock()
  val, ok := stSessid[id].Storage
  return val, ok
}

func Set(id string, value Sessid) error {
  _, dublicate := ramGet(id)
  if dublicate {
    return errors.New("Такой ключ уже есть")
  }
  r := &sync.Mutex{}
  r.Lock()
  defer r.Unlock()
  stSessid[id].Storage = value
}

func Del(id string) error {
  _, dublicate := ramGet(id)
  if !dublicate {
    return errors.New("Ключа нету")
  }
  r := &sync.Mutex{}
  r.Lock()
  defer r.Unlock()
  delete(stSessid, id)
}
func Up(id string, value Sessid) error {
  _, dublicate := ramGet(id)
  if !dublicate {
    return errors.New("Ключа нету")
  }
  r := &sync.Mutex{}
  r.Lock()
  defer r.Unlock()
  stSessid[id].Storage = value
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Pavlyuk, 2016-08-03
@VGrabko

A little copy-paste, it's normal for go.
You have another terrible problem here, you create mutexes as local variables. As a result, they are useless here.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question