--
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...@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.
func test() int8 {
panic("hell")
return 2
}
...
fmt.Println(unsafe.Sizeof(test()))
`unsafe` is a magic package. It is provided by the language and entirely implemented as compiler-intrinsics - and at compile-time. In particular, the result of `unsafe.Sizeof` must be a compile-time constant. 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 <wangcha...@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 []intfmt.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 golan...@googlegroups.com.