douyue7408 2019-01-18 12:56
浏览 37
已采纳

重新分配指针函数参数

I'm trying to reassign a pointer to a new value that's been passed on as func parameter, but once I step out of the function, the pointer has a nil value again.

I've stepped through the code and it seems to work until I step out of the function into the calling function where the passed on pointer still holds a NIL value.

func Prepare(db *sqlx.DB, stmt *sqlx.Stmt, query String) error {
    res,err := db.PreparexStatement(context.Background(), query)
    stmt = res
    return err
}

I would expect the following to work:

func Boot(db *sqlx.DB, stmt *sqlx.Stmt, query String) {
    err := Prepare(db, stmt, query)
    if err != nil {
        //handle
    }
}

I'm still fairly new to GO, so I believe I fail to grasp a concept here. Any help would be greatly appreciated.

  • 写回答

1条回答 默认 最新

  • duanjianxiu9400 2019-01-18 13:06
    关注

    Parameters act as local variables inside a function. You may change their values, but that only changes the values of the local variables. They are independent from the variables whose value you pass.

    If you want to modify something, you have to pass its address, and modify the pointed value, e.g.:

    func main() {
        var x int
        fmt.Println("before", x)
        set(&x)
        fmt.Println("after", x)
    }
    
    func set(i *int) {
        *i = 2
    }
    

    Outputs (try it on the Go Playground):

    before 0
    after 2
    

    The same thing applies to pointer variables: if you want to modify a pointer variable, you again have to pass its address, and modify the pointed value. In case of a pointer, the type will be a pointer to pointer, e.g.:

    func main() {
        var x *int
        x = new(int)
        fmt.Println("before", x, *x)
        set(&x)
        fmt.Println("after", x, *x)
    }
    
    func set(i **int) {
        p := new(int)
        *p = 2
        *i = p
    }
    

    Output (try it on the Go Playground):

    before 0x416020 0
    after 0x416040 2
    

    Of course if you have a pointer variable and don't want to modify the pointer value just the pointed value, you can just pass the pointer, and modify the pointed value:

    func main() {
        var x *int
        x = new(int)
        fmt.Println("before", x, *x)
        set(x)
        fmt.Println("after", x, *x)
    }
    
    func set(i *int) {
        *i = 2
    }
    

    Output (try it on the Go Playground):

    before 0x416020 0
    after 0x416020 2
    

    One thing to note: you can't pass a nil pointer to a function and expect to be able to assign anything to the pointed value: nil pointers point to nowhere. You have to pass a non-nil pointer, or as an alternative you may return the pointer value (and assign it at the caller).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图