Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. )
  8.  
  9. func main() {
  10. fmt.Println("Hello, playground")
  11. data := []byte{65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 0, 0, 0, 0, 0, 0, 0, 0}
  12. fileNm := "test"
  13. ioutil.WriteFile(fileNm, data, 0644)
  14. rslt, _ := ioutil.ReadFile(fileNm)
  15. fmt.Println("string:", string(rslt), ", []byte:", rslt, ", length:", len(rslt))
  16. f, _ := os.Open(fileNm)
  17. defer f.Close()
  18. info, _ := f.Stat()
  19. lo := int64(0)
  20. hi := info.Size() - 1
  21. var buf [1]byte
  22. for lo <= hi {
  23. mid := (lo + hi) / 2
  24. f.ReadAt(buf[:], mid)
  25. if buf[0] == byte(0) {
  26. hi = mid - 1
  27. } else {
  28. lo = mid + 1
  29. }
  30. }
  31. if lo != info.Size() {
  32. os.Truncate(fileNm, lo)
  33. }
  34. rslt, _ = ioutil.ReadFile(fileNm)
  35. fmt.Println("string:", string(rslt), ", []byte:", rslt, ", length:", len(rslt))
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement