重新分配一个变量而不需要创建临时变量。

huangapple go评论64阅读模式
英文:

Reassign a variable without having to create a temporary variable

问题

我正在学习Golang,之前使用的是Python。

下面的函数类似于Python的pop()方法,它可以从列表(或在Go中称为切片)中删除给定索引处的项,并返回被删除的项。

func popElement(indexOfElement int, slice []int) (int, []int) {
	element := slice[indexOfElement]
	newSlice := append(slice[:indexOfElement], slice[indexOfElement+1:]...)
	return element, newSlice
}

然后,我想将这个函数作为下面排序函数的一部分使用。但是我必须创建一个临时变量newSlice

func sortSlice(sliceToSort []int) []int {
	var sortedSlice []int
	for 1 <= len(sliceToSort) {
		indexOfSmallest := findIndexOfSmallest(sliceToSort)
		smallestElement, newSlice := popElement(indexOfSmallest, sliceToSort)
		sliceToSort = newSlice
		sortedSlice = append(sortedSlice, smallestElement)
	}
	return sortedSlice
}

有没有一种方法可以在不创建临时变量newSlice的情况下实现相同的结果?

类似于:

func sortSlice(sliceToSort []int) []int {
    var sortedSlice []int
    for 1 <= len(sliceToSort) {
        indexOfSmallest := findIndexOfSmallest(sliceToSort)
        smallestElement, sliceToSort = popElement(indexOfSmallest, sliceToSort)
        sortedSlice = append(sortedSlice, smallestElement)
    }
    return sortedSlice
}
英文:

I'm learning Golang and coming from Python.

The following function is similar to python's pop() method that removes an item at a given index from a list (or slice in Go) and returns the removed item.

func popElement(indexOfElement int, slice []int) (int, []int) {
	element := slice[indexOfElement]
	newSlice := append(slice[:indexOfElement], slice[indexOfElement+1:]...)
	return element, newSlice
}

I would then like to use this function as part of the below sorting function. But I have to create a temporary variable newSlice

func sortSlice(sliceToSort []int) []int {
	var sortedSlice []int
	for 1 &lt;= len(sliceToSort) {
		indexOfSmallest := findIndexOfSmallest(sliceToSort)
		smallestElement, newSlice := popElement(indexOfSmallest, sliceToSort)
		sliceToSort = newSlice
		sortedSlice = append(sortedSlice, smallestElement)
	}
	return sortedSlice
}

Is there a way to achieve the same result, without having to create the temporary newSlice variable?

Something like:

func sortSlice(sliceToSort []int) []int {
    var sortedSlice []int
    for 1 &lt;= len(sliceToSort) {
        indexOfSmallest := findIndexOfSmallest(sliceToSort)
        smallestElement, sliceToSort = popElement(indexOfSmallest, sliceToSort)
        sortedSlice = append(sortedSlice, smallestElement)
    }
    return sortedSlice
}

答案1

得分: 3

根据 @mkopriva 的建议,需要预先声明 smallestElement。

func sortSlice(sliceToSort []int) []int {
        var sortedSlice []int
        var smallestElement int
        for 1 <= len(sliceToSort) {
                indexOfSmallest := findIndexOfSmallest(sliceToSort)
                smallestElement, sliceToSort = popElement(indexOfSmallest, sliceToSort)
                sortedSlice = append(sortedSlice, smallestElement)
        }
        return sortedSlice
}
英文:

As @mkopriva suggested smallestElement needs to be predeclared.

func sortSlice(sliceToSort []int) []int {
        var sortedSlice []int
        var smallestElement int
        for 1 &lt;= len(sliceToSort) {
                indexOfSmallest := findIndexOfSmallest(sliceToSort)
                smallestElement, sliceToSort = popElement(indexOfSmallest, sliceToSort)
                sortedSlice = append(sortedSlice, smallestElement)
        }
        return sortedSlice
}

huangapple
  • 本文由 发表于 2022年8月27日 21:06:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/73511259.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定