string.go 941 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package session
  2. // IsSetString to check if variable exists
  3. func (s *Session) IsSetString(name string) bool {
  4. s.varlist.RLock()
  5. defer s.varlist.RUnlock()
  6. if _, ok := s.varlist.String[name]; ok {
  7. return true
  8. } else {
  9. return false
  10. }
  11. }
  12. // GetString returns stored variable value or default
  13. func (s *Session) GetString(name string, def string) string {
  14. s.varlist.RLock()
  15. defer s.varlist.RUnlock()
  16. if v, ok := s.varlist.String[name]; ok {
  17. return v
  18. } else {
  19. return def
  20. }
  21. }
  22. // SetString to set variable value
  23. func (s *Session) SetString(name string, value string) {
  24. isset := s.IsSetString(name)
  25. s.varlist.Lock()
  26. defer s.varlist.Unlock()
  27. s.varlist.String[name] = value
  28. if isset || value != "" {
  29. s.changed = true
  30. }
  31. }
  32. // DelString to remove variable
  33. func (s *Session) DelString(name string) {
  34. if s.IsSetString(name) {
  35. s.varlist.Lock()
  36. defer s.varlist.Unlock()
  37. delete(s.varlist.String, name)
  38. s.changed = true
  39. }
  40. }