一个函数是否可以改变在其他地方声明的字符串数组切片的大小?Go语言

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

Is it possible for a function to change the size of a slice of string array declared elsewhere? golang

问题

我想要在切片中就地删除一个元素。但是当我这样做时,会在底层数组中产生两个空元素。
我已经在这里这里进行了搜索。

package main

import "fmt"

//String remove adjacent duplicates from a string array
func rmDup(str []string) []string {
	for i := 1; i < len(str); i++ {
		if str[i] == str[i-1] {
			copy(str[i:], str[i+1:])
			str[len(str)-1] = ""
			str = str[:len(str)-1]
		}
	}
	return str
}

func main() {

	str := []string{"dog", "cat", "cat", "mouse", "mouse", "zebra", "zebra"}

	fmt.Println(str)
	newstr := rmDup(str)
	fmt.Println(str)
	fmt.Println(newstr)
	fmt.Println(len(str), cap(str), "final")
	fmt.Println(len(newstr), cap(newstr), "final")
}

main函数中,有没有办法让str返回rmDup函数中定义的sizecapacity呢?

英文:

I would like to remove an element from the slice in place. But when I do this I end up producing two empty elements in the underlying array.
I already searched here, here

package main

import &quot;fmt&quot;

//String remove adjacent duplicates from a string array
func rmDup(str []string) []string {
	for i := 1; i &lt; len(str); i++ {
		if str[i] == str[i-1] {
			copy(str[i:], str[i+1:])
			str[len(str)-1] = &quot;&quot;
			str = str[:len(str)-1]
		}
	}
	return str
}

func main() {

	str := []string{&quot;dog&quot;, &quot;cat&quot;, &quot;cat&quot;, &quot;mouse&quot;, &quot;mouse&quot;, &quot;zebra&quot;, &quot;zebra&quot;}

	fmt.Println(str)
	newstr := rmDup(str)
	fmt.Println(str)
	fmt.Println(newstr)
	fmt.Println(len(str), cap(str), &quot;final&quot;)
	fmt.Println(len(newstr), cap(newstr), &quot;final&quot;)
}

Is there any way that str in main can return the size and capacity defined in rmDup()
1: https://github.com/golang/go/wiki/SliceTricks
2: https://blog.golang.org/go-slices-usage-and-internals

答案1

得分: 3

原来我能够自己找到答案。由于Go语言通过值进行函数调用,所以无法在另一个作用域中更改已声明的切片,除非使用指针。

package main

import "fmt"

//String remove adjacent duplicates from a string array
func rmDup(str *[]string) []string {
	var s = *str
	for i := 1; i < len(s); i++ {
		if s[i] == s[i-1] {
			copy(s[i:], s[i+1:])
			s[len(s)-1] = ""
			s = s[:len(s)-1]
		}
	}
	*str = s
	return s
}

func main() {

	str := []string{"dog", "cat", "cat", "mouse", "mouse", "zebra",
		"zebra"}

	fmt.Println(str)
	newstr := rmDup(&str)
	fmt.Println(str)
	fmt.Println(newstr)
	fmt.Println(len(str), cap(str), "final")
	fmt.Println(len(newstr), cap(newstr), "final")
}

输出:

[dog cat cat mouse mouse zebra zebra]
[dog cat mouse zebra]
[dog cat mouse zebra]
4 7 final
4 7 final
英文:

It turned out that I was able to find the answer myself. Since the Go language performs function calls by value it is impossible to change a slice declared in another scope, except using pointers.

package main

import &quot;fmt&quot;

//String remove adjacent duplicates from a string array
func rmDup(str *[]string) []string {
 var s = *str
 for i := 1; i &lt; len(s); i++ {
	if s[i] == s[i-1] {
		copy(s[i:], s[i+1:])
		s[len(s)-1] = &quot;&quot;
		s = s[:len(s)-1]
	}
}
*str = s
return s
}

func main() {

  str := []string{&quot;dog&quot;, &quot;cat&quot;, &quot;cat&quot;, &quot;mouse&quot;, &quot;mouse&quot;, &quot;zebra&quot;, 
  &quot;zebra&quot;}

  fmt.Println(str)
  newstr := rmDup(&amp;str)
  fmt.Println(str)
  fmt.Println(newstr)
  fmt.Println(len(str), cap(str), &quot;final&quot;)
  fmt.Println(len(newstr), cap(newstr), &quot;final&quot;)
}

Output:

[dog cat cat mouse mouse zebra zebra]
[dog cat mouse zebra]
[dog cat mouse zebra]
4 7 final
4 7 final

答案2

得分: 2

main函数中,有没有办法让str返回rmDup()函数中定义的大小和容量呢?

例如,

package main

import "fmt"

// 从字符串数组中删除相邻重复的字符串
func rmDup(str []string) []string {
    for i := 1; i < len(str); i++ {
        if str[i] == str[i-1] {
            copy(str[i:], str[i+1:])
            str[len(str)-1] = ""
            str = str[:len(str)-1]
        }
    }
    return str
}

func main() {
    str := []string{"dog", "cat", "cat", "mouse", "mouse", "zebra", "zebra"}
    fmt.Println(len(str), cap(str), str)
    str = rmDup(str)
    fmt.Println(len(str), cap(str), str)
}

输出结果:

7 7 [dog cat cat mouse mouse zebra zebra]
4 7 [dog cat mouse zebra]

我是说在不让rmDup()函数返回任何内容的情况下,有没有办法在原地修改str呢?

例如,

package main

import "fmt"

// 从字符串数组中删除相邻重复的字符串
func rmDup(str *[]string) {
    s := *str
    for i := 1; i < len(s); i++ {
        if s[i] == s[i-1] {
            copy(s[i:], s[i+1:])
            s[len(s)-1] = ""
            s = s[:len(s)-1]
        }
    }
    *str = s
}

func main() {
    str := []string{"dog", "cat", "cat", "mouse", "mouse", "zebra", "zebra"}
    fmt.Println(len(str), cap(str), str)
    rmDup(&str)
    fmt.Println(len(str), cap(str), str)
}

输出结果:

7 7 [dog cat cat mouse mouse zebra zebra]
4 7 [dog cat mouse zebra]
英文:

> Is there any way that str in main can return the size and capacity
> defined in rmDup()?

For example,

package main

import &quot;fmt&quot;

//String remove adjacent duplicates from a string array
func rmDup(str []string) []string {
	for i := 1; i &lt; len(str); i++ {
		if str[i] == str[i-1] {
			copy(str[i:], str[i+1:])
			str[len(str)-1] = &quot;&quot;
			str = str[:len(str)-1]
		}
	}
	return str
}

func main() {
	str := []string{&quot;dog&quot;, &quot;cat&quot;, &quot;cat&quot;, &quot;mouse&quot;, &quot;mouse&quot;, &quot;zebra&quot;, &quot;zebra&quot;}
	fmt.Println(len(str), cap(str), str)
	str = rmDup(str)
	fmt.Println(len(str), cap(str), str)
}

Output:

7 7 [dog cat cat mouse mouse zebra zebra]
4 7 [dog cat mouse zebra]

> i meant in place without the function rmDup() having to return
> anything.

For example,

package main

import &quot;fmt&quot;

//String remove adjacent duplicates from a string array
func rmDup(str *[]string) {
	s := *str
	for i := 1; i &lt; len(s); i++ {
		if s[i] == s[i-1] {
			copy(s[i:], s[i+1:])
			s[len(s)-1] = &quot;&quot;
			s = s[:len(s)-1]
		}
	}
	*str = s
}

func main() {
	str := []string{&quot;dog&quot;, &quot;cat&quot;, &quot;cat&quot;, &quot;mouse&quot;, &quot;mouse&quot;, &quot;zebra&quot;, &quot;zebra&quot;}
	fmt.Println(len(str), cap(str), str)
	rmDup(&amp;str)
	fmt.Println(len(str), cap(str), str)
}

Output:

7 7 [dog cat cat mouse mouse zebra zebra]
4 7 [dog cat mouse zebra]

huangapple
  • 本文由 发表于 2017年3月26日 00:23:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/43018862.html
匿名

发表评论

匿名网友

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

确定