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

萝らか妹 提交于 2021-01-21 06:42:08

问题


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

//go:nosplit
func throw(s string) {
    // Everything throw does should be recursively nosplit so it
    // can be called even when it's unsafe to grow the stack.
    systemstack(func() {
        print("fatal error: ", s, "\n")
    })
    gp := getg()
    if gp.m.throwing == 0 {
        gp.m.throwing = 1
    }
    fatalthrow()
    *(*int)(nil) = 0 // not reached
}

What does *(*int)(nil) = 0 means? and since this line *(*int)(nil) = 0 could NOT be reached, why it is here? any special usage?


回答1:


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")
    }
}


来源:https://stackoverflow.com/questions/64751440/what-does-intnil-0-mean-in-golang

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!