config.go 761 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package wrapper
  2. import (
  3. "encoding/json"
  4. "os"
  5. )
  6. type Config struct {
  7. Blog struct {
  8. Pagination struct {
  9. Index int
  10. Category int
  11. }
  12. }
  13. }
  14. func configNew() *Config {
  15. c := &Config{}
  16. c.configDefault()
  17. return c
  18. }
  19. func (this *Config) configDefault() {
  20. this.Blog.Pagination.Index = 5
  21. this.Blog.Pagination.Category = 5
  22. }
  23. func (this *Config) configRead(file string) error {
  24. f, err := os.Open(file)
  25. if err != nil {
  26. return err
  27. }
  28. defer f.Close()
  29. dec := json.NewDecoder(f)
  30. return dec.Decode(this)
  31. }
  32. func (this *Config) configWrite(file string) error {
  33. r, err := json.Marshal(this)
  34. if err != nil {
  35. return err
  36. }
  37. f, err := os.Create(file)
  38. if err != nil {
  39. return err
  40. }
  41. defer f.Close()
  42. _, err = f.WriteString(string(r))
  43. return err
  44. }