go - 在循环中清除和重写 slice

标签 go slice

我想知道在我的用例中删除 slice 的“最”正确方法是什么。我有一个 for 循环,我可以在其中运行返回一个 slice 的函数,然后将该 slice 附加到一个更大的 slice 。每次调用 for 循环时,较小的 slice 应该为空。我不能只用返回的值覆盖 slice ,因为我需要知道长度。

我得到了预期的输出,但不知道我是否会遇到内存泄漏或获取错误数据的错误。 最好将 slice 设置为 nil、创建新 slice 还是其他?

https://play.golang.org/p/JxMKaFQAPWL

package main

import (
    "fmt"
)

func populateSlice(offset int) []string {
    letters := []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "OP", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
    toReturn := make([]string, 0)

    if len(letters)-offset <= 0 {
        toReturn = nil
    } else if len(letters) < offset+10 {
        remaining := len(letters) - offset
        toReturn = letters[offset:remaining+offset]
    } else {
        toReturn = letters[offset:10+offset]
    }

    fmt.Printf("toReturn: %#v\n", toReturn)
    return toReturn

}

func main() {

    offset := 0
    bigSlice := make([]string, 0)

    for {

        smallSlice := populateSlice(offset)

        bigSlice = append(bigSlice, smallSlice...)

        if smallSlice == nil || len(smallSlice) < 5 {
            fmt.Printf("break: len(smallSlice): %v", len(smallSlice))
            break
        }
        offset += len(smallSlice)

        fmt.Printf("smallSlice: %#v\n", smallSlice)
        fmt.Printf("bigSlice: %#v\n\n", bigSlice)
    }
}

最佳答案

首先,简化你的代码,

package main

import "fmt"

func populateSlice(offset int) []string {
    letters := []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "OP", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
    lo, hi := offset, offset+10
    if hi > len(letters) {
        hi = len(letters)
    }
    if lo < 0 || lo >= hi {
        return nil
    }
    return letters[lo:hi:hi]
}

func main() {
    var bigSlice []string
    for offset := 0; ; {
        smallSlice := populateSlice(offset)
        fmt.Printf("smallSlice: %#v\n", smallSlice)
        if len(smallSlice) == 0 {
            break
        }
        bigSlice = append(bigSlice, smallSlice...)
        offset += len(smallSlice)
    }
    bigSlice = bigSlice[:len(bigSlice):len(bigSlice)]
    fmt.Printf("bigSlice: %#v\n", bigSlice)
}

Playground :https://play.golang.org/p/sRqazV_luol

输出:

smallSlice: []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"}
smallSlice: []string{"k", "l", "m", "n", "OP", "q", "r", "s", "t", "u"}
smallSlice: []string{"v", "w", "x", "y", "z"}
smallSlice: []string(nil)
bigSlice: []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "OP", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}

没有要删除的 slice 。没有内存泄漏。 Go 有一个垃圾收集器。没有坏数据。

关于go - 在循环中清除和重写 slice ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51938303/

相关文章:

json - 从golang中的json数组中删除特定元素

Python,切片列表,步骤并仅提取第一个字符

facebook - 无法使用包 "golang.org/x/oauth2"向 facebook : "Missing redirect_uri parameter" 进行身份验证

go - 使用 slack bot 处理多个数据竞争

go - 将上下文传递给 gorilla mux - go 习语

python - 将单个值分配给切片的大多数 pythonic 和/或高性能方法?

java - 从一个主数组创建子数组

python从字符串创建切片对象

python - 用另一个列表切片列表

go - 未定义 : proto. ProtoPackageIsVersion3