Thanks for the response, which makes sense. `unsafe.Sizeof` is indeed a 
magic package. The parameter of this function isn't evaluated at all, for 
example, the following code will not generate panic either, because the 
function test isn't executed at all. 

func test() int8 {
    panic("hell")
    return 2
}

...
fmt.Println(unsafe.Sizeof(test()))



在 2020年7月27日星期一 UTC+8下午11:09:56,Axel Wagner写道:
>
> `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 <wangcha...@gmail.com 
> <javascript:>> 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 golan...@googlegroups.com <javascript:>.
>> 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/1d9552ea-65a0-4a1e-a1a9-c3c5aa78b092o%40googlegroups.com.

Reply via email to