What does *(*int)(nil) = 0 mean in golang?

后端 未结 1 1562
南方客
南方客 2021-02-19 11:29

I notice there is one line *(*int)(nil) = 0 in function throw

//go:nosplit
func throw(s string) {
    // E         


        
1条回答
  •  渐次进展
    2021-02-19 12:30

    The line:

    *(*int)(nil) = 0
    

    Tries to dereference a nil pointer and assign a value to it, which is always a runtime panic. The code should not reach this line, ever, but if it would anyhow (e.g. a faulty code change in the future), it would panic so the error could be detected and wouldn't go unnoticed.

    It's common sense to do something similar in your code too, but with a more obvious "construct" such as panic("unreachable"). For example:

    func sign(a int) string {
        switch {
        case a > 0:
            return "Positive"
        case a < 0:
            return "Negative"
        case a == 0:
            return "Zero"
        default:
            panic("unreachable")
        }
    }
    

    Note that in this example this is not just to detect errors early, it's also a requirement because to the compiler there would be no guarantee that a return statement would be reached. You could also move the panic("unreachable") statement after the switch (instead of the default branch), it's a matter of taste.

    If you would change the above function to not return but print the sign, it would still be a good practice to leave the default branch to panic, although it's not a requirement in this variant:

    func printSign(a int) {
        switch {
        case a > 0:
            fmt.Println("Positive")
        case a < 0:
            fmt.Println("Negative")
        case a == 0:
            fmt.Println("Zero")
        default:
            panic("unreachable")
        }
    }
    

    0 讨论(0)
提交回复
热议问题