`unsafe` is a magic package. It is provided by the language <https://golang.org/ref/spec#Package_unsafe> and entirely implemented as compiler-intrinsics - and at compile-time. In particular, the result of `unsafe.Sizeof` must be a compile-time constant <https://golang.org/ref/spec#Constants>. In general, the compiler can't determine if the evaluation of an index-expression panics at runtime (it could in the case you mention, but `a` might also be dynamic). And there is little use in having it panic - at the end of the day, what it does is still pretty well-defined. So, panicing would require a) extra work (both in spec and implementation) and b) be strictly less useful. So why should it?
On Mon, Jul 27, 2020 at 4:38 PM Benjamin <wangchao.nlp....@gmail.com> wrote: > The code is something like below, the a is a nil slice. I am curious why > it doesn't generate panic. Could anyone educate me on this? Thanks. > > var a []int > fmt.Println(unsafe.Sizeof(a[0])) > > > > -- > 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. > To view this discussion on the web visit > https://groups.google.com/d/msgid/golang-nuts/5d236b5b-aeec-4fb3-a993-19acf81ced6ao%40googlegroups.com > <https://groups.google.com/d/msgid/golang-nuts/5d236b5b-aeec-4fb3-a993-19acf81ced6ao%40googlegroups.com?utm_medium=email&utm_source=footer> > . > -- 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. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAEkBMfEpap6P1FD00LOShSHqbhzJK2Nbs%3DRO-E0x6YeyMAkN8w%40mail.gmail.com.