Address of a temporary in Go?

后端 未结 8 900
不知归路
不知归路 2021-02-01 13:24

What\'s the cleanest way to handle a case such as this:

func a() string {
    /* doesn\'t matter */
}

b *string = &a()

This generates the

8条回答
  •  不要未来只要你来
    2021-02-01 14:08

    In the end you are proposing that Go should allow you to take the address of any expression, for example:

    i,j := 1,2
    var p *int = &(i+j)
    println(*p)
    

    The current Go compiler prints the error: cannot take the address of i + j

    In my opinion, allowing the programmer to take the address of any expression:

    • Doesn't seem to be very useful (that is: it seems to have very small probability of occurrence in actual Go programs).
    • It would complicate the compiler and the language spec.

    It seems counterproductive to complicate the compiler and the spec for little gain.

提交回复
热议问题