12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- package main
- import (
- "fmt"
- "strings"
- "unsafe"
- )
- type slice struct {
- arrayPtr uint
- len uint
- cap uint
- }
- func (s slice) Data() []byte {
- var p *byte
- var i uint
- res := make([]byte, s.len)
- for i = 0; i < s.len; i++ {
- p = (*byte)(unsafe.Pointer(uintptr(s.arrayPtr) + uintptr(i)))
- res[i] = *p
- }
- return res
- }
- func Dump(slicePtr *[]byte) {
- var n uintptr = uintptr(unsafe.Pointer(slicePtr))
- var s *slice = (*slice)(unsafe.Pointer(n))
- fmt.Println(*s)
- fmt.Printf("Len: %d, Cap: %d: Data:\n", s.len, s.cap)
- fmt.Printf("%#v\n", s.Data())
- }
- func main() {
- var max byte = 13
- a := make([]byte, max, 64)
- var i byte
- for i = 0; i < max; i++ {
- a[i] = '@' + i
- }
- fmt.Println(a, len(a), cap(a))
- Dump(&a)
- var z int
- fmt.Println(unsafe.Sizeof(z))
- r := strings.NewReader("ceci est une chaîne")
- var s []byte
- n, err := r.ReadAt(s, 4)
- fmt.Println("Read: %n (%v): %#v\n", n, err, s)
- }
|