Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "errors"
  5. "fmt"
  6. "io/ioutil"
  7. "os"
  8. "strings"
  9. )
  10.  
  11. func getConfigMap(configFile string) (configMap map[string]string, err error) {
  12. // get file stats
  13. var stat os.FileInfo
  14. stat, err = os.Stat(configFile)
  15. if err != nil {
  16. return
  17. }
  18.  
  19. if stat.IsDir() {
  20. err = errors.New(configFile + " is a directory")
  21. return
  22. }
  23.  
  24. // reading the file
  25. var data []byte
  26. data, err = ioutil.ReadFile(configFile)
  27. if err != nil {
  28. return
  29. }
  30.  
  31. lines := strings.Split(string(data), "\n")
  32.  
  33. configMap = make(map[string]string)
  34. for _, line := range lines {
  35. ei := strings.Index(line, "=")
  36. if ei == -1 {
  37. break
  38. }
  39. configMap[line[:ei]] = line[ei+1:]
  40. }
  41.  
  42. return
  43. }
  44.  
  45. func main() {
  46. fmt.Println(getConfigMap("test.properties"))
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement