On Thu, Mar 29, 2018 at 10:13 AM, Nilsocket <nilsoc...@gmail.com> wrote: > >> > https://research.swtch.com/godata > > I did read it, but what I was asking is irrelevent to it.
I guess I don't see why. It seems to exactly answer your question. > func showInt(x int) { > res := (*[unsafe.Sizeof(x)]byte)(unsafe.Pointer(&x)) > for i := range res { > fmt.Printf("%.2x ", res[i]) > } > fmt.Println() > } > // showInt(12345) > // 39 30 00 00 00 00 00 00 > > An integer in it's binary form is stored like above, little endian. > What I mean is how, strings are represented, while converting a string to > []byte, gives me utf-8 values, > but directly ascessing the underlying memory of string, without converting > it to []byte shows different things. Because, as described at research.swtch.com/godata, a string is stored as a pointer and a length, and when you use unsafe to look at the contents of a string value you are looking at the pointer and the length. If you want to look at the actual contents of the string--the bytes that correspond to the bytes stored in a []byte--you need to indirect through the string pointer. See also reflect.StringHeader. >> > Because in Go the size of an array must be a constant. >> > >> > Ian > > > > An array size must be constant, but my real question is? > How come, a uintptr type value returned by unsafe.Sizeof(x), is considered > as const? > and doesn't accept, if uintptr type value is returned by someother function. > I mean, how was it able to differentiate between someother function and > unsafe.Sizeof(x). Because unsafe.Sizeof is a special case, as documented at https://golang.org/ref/spec#Package_unsafe . Ian -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.