57942907ad8cfe15c03c7737d604f06689a19a96.svn-base 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package main
  2. import (
  3. "encoding/binary"
  4. "errors"
  5. "fmt"
  6. "github.com/recoilme/slowpoke"
  7. "os"
  8. "os/exec"
  9. "path/filepath"
  10. "runtime"
  11. "strings"
  12. )
  13. func main(){
  14. file := "pos/pos.db"
  15. // close all opened database
  16. // init key/val
  17. key := []byte("startPos")
  18. //val := []byte("bar")
  19. id := make([]byte, 4)
  20. binary.BigEndian.PutUint32(id, uint32(10))
  21. defer slowpoke.Close("pos/pos.db")
  22. //for i:=0;i<10 ; i++ {
  23. // slowpoke.Set(file, []byte(strconv.Itoa(i)) , []byte(strconv.Itoa(i)))
  24. //}
  25. ////slowpoke.Close("pos/pos.db")
  26. //store
  27. //err :=slowpoke.Set(file, []byte("startPos"), []byte("startPos"))
  28. //if err != nil{
  29. // fmt.Println(err.Error())
  30. //}
  31. // get
  32. res, err := slowpoke.Get(file, key)
  33. if err != nil {
  34. println(err.Error())
  35. }
  36. path,_ :=GetCurrentPath()
  37. fmt.Println(string(res),path)
  38. //res, err = slowpoke.Get(file, []byte("4"))
  39. //
  40. //if err != nil {
  41. // println(err.Error())
  42. //}
  43. ////path,_ :=GetCurrentPath()
  44. //
  45. //fmt.Println(string(res))
  46. }
  47. func GetCurrentPath() (string, error) {
  48. file, err := exec.LookPath(os.Args[0])
  49. if err != nil {
  50. return "", err
  51. }
  52. path, err := filepath.Abs(file)
  53. if err != nil {
  54. return "", err
  55. }
  56. //fmt.Println("path111:", path)
  57. if runtime.GOOS == "windows" {
  58. path = strings.Replace(path, "\\", "/", -1)
  59. }
  60. //fmt.Println("path222:", path)
  61. i := strings.LastIndex(path, "/")
  62. if i < 0 {
  63. return "", errors.New(`Can't find "/" or "\".`)
  64. }
  65. //fmt.Println("path333:", path)
  66. return string(path[0 : i+1]), nil
  67. }