Casting from one pointer to pointer type to another in Golang error

后端 未结 2 477
逝去的感伤
逝去的感伤 2021-02-02 15:02

Can anyone tell my why this wouldn\'t compile?

package main

type myint int
func set(a **myint) {
    i := myint(5)
    *a = &i 
}

func main() {
    var k *         


        
2条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-02 15:41

    Here are two functionally equivalent working versions of your program.

    package main
    
    type mypint *int
    
    func set(a *mypint) {
        i := int(5)
        *a = &i
    }
    
    func main() {
        var k *int
        set((*mypint)(&k))
        print(*k)
    }
    

    http://play.golang.org/p/l_b9LBElie

    package main
    
    type myint int
    
    func set(a *myint) *myint {
        i := myint(5)
        a = &i
        return a
    }
    
    func main() {
        var k *int
        k = (*int)(set((*myint)(k)))
        print(*k)
    }
    

    http://play.golang.org/p/hyaPFUNlp8

提交回复
热议问题