美文网首页
记一次golang slice作为函数参数调用时的坑

记一次golang slice作为函数参数调用时的坑

作者: 舒小贱 | 来源:发表于2020-02-20 16:40 被阅读0次

    场景:需要删除slice中第一个元素
    stackoverflow上给出解决办法:

    Where a is the slice, and i is the index of the element you want to delete:
    
    a = append(a[:i], a[i+1:]...)
    ... is syntax for variadic arguments in Go.
    

    经测试有效:

    s := []byte("helloworld")
    s = append(s[:0], s[1:]...)
    fmt.println(string(s))//输出结果为:elloworld
    

    但是当我用函数调用的方式来删除slice中一个元素时,出现了非预期的现象:

    s := []byte("helloworld")
    dosomething(s)
    fmt.Println(string(s))//输出异常结果:elloworldd
    
    func dosomething(s []byte){
        s = append(s[:0], s[1:]...)
        //fmt.Println(string(s)) 会输出正常的 elloworld
    }
    

    发现:将slice作为参数,进行函数调用时,函数中s的值发生改变(在调用函数呃内[]byte符合预期的删除了第一个byte),但是调用方获取[]byte时,既不是预期删除了第一个byte的elloworld,也不是原始值helloworld,而是删除了第一个元素的elloworld和第最后一个元素的d的拼凑体。why??

    以上预期是基于slice是引用传递,调用函数内对slice的改变,会反应给调用方,但是上面的例子表现却出乎我们意料。

    改变思路,将调用函数中[]byte的改变后结果作为函数返回值返回给被调用方:

    s := []byte("helloworld")
    s = dosomething(s)
    fmt.Println(string(s)) //输出 elloworld
    
    func dosomething(s []byte) []byte {
        s = append(s[:0], s[1:]...)
        // fmt.Println(string(s)) 输出 elloworld
        return s
    }
    

    作为返回值返回时,才符合我们预期。看来,slice作为函数参数引用传递时,是有坑的。

    相关文章

      网友评论

          本文标题:记一次golang slice作为函数参数调用时的坑

          本文链接:https://www.haomeiwen.com/subject/fpkpzctx.html