How do I create a slice of one item in Go?

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

How do I create a slice of one item in Go?

问题

让我们假设有一个函数接受一个字符串切片:

func Join(strs []string) {
    ...
}

我有一个单独的字符串:

a := "y'all ain't got the honey nut?"

如何将该字符串转换为切片呢?

英文:

Let's say a function takes a slice of strings:

func Join(strs []string) {
     ...
}

I have a single string:

a := "y'all ain't got the honey nut?"

How can I convert that string into a slice?

答案1

得分: 35

你可以使用以下约定创建一个只包含一个元素的切片:

a := "你们没有蜂蜜坚果吗?"
singleItemArray := []string{a}

strings.Join(singleItemArray)。
英文:

You can create a slice of one item using the following convention:

a := "y'all ain't got the honey nut?"
singleItemArray := []string{a}

strings.Join(singleItemArray);

答案2

得分: 11

你的问题的实际答案就像[]string{"string"}一样简单,正如miltonb所说。

但是我想指出的是,在Go语言中编写和使用可变参数函数是多么容易,即一个带有可变数量参数的函数。

你可以将函数的签名改为F(a ...string)。然后,在函数F中,a是一个切片,你可以像这样调用它:F("a")F("a", "b")。当你实际上有一个切片或数组时,你可以通过调用F(a...)将其传递给F

不确定这种语法是否适合你的工作,但我想让你知道这是一个选项。

英文:

The actual answer to your question is as simple as []string{"string"}, as miltonb said.

But what I wanted to point out is how easy it is to write and use a variadic function in Go, a function with a variable number of arguments.

You can change signature of your function to F(a ...string). Then, a is slice in the function F, and you can call it like F("a") and F("a", "b"). And when you actually have a slice or array, you can pass it to F by calling F(a...).

Not sure if this syntax fits your job, but I wanted to let you know about it as an option.

答案3

得分: 2

这个问题实际上涉及到数组和切片。问题文本是关于数组的,而代码则是用切片进行示例。因此,有两个问题需要回答:传递一个单个元素的切片,以及传递一个单个元素的数组。

一个数组:var a [1]string
一个切片:var s []string

单个元素的切片传递给函数:

func SliceFunc(slc []string) {
    fmt.Println(slc)
}

func main() {
    a := "stringy"
    SliceFunc([]string{a})
    // 或者将一个实际的数组传递给同一个函数
    b := [...]string{"thingy"}
    SliceFunc([]string{b[0]})
}

单个元素的数组传递给函数。

在这里有一个问题,因为数组具有固定的长度,并且作为函数的参数时不能接受不同长度的数组,所以我们只能使用具有有限灵活性的工作函数:

func ArrayFunc(arr [1]string) {
    fmt.Println(slc)
}

func main() {
    var a [1]string
    a[0] = "stringy"
    ArrayFunc(a)
}

总的来说,似乎使用切片作为一般化的解决方案更加灵活。

(如果你想了解更多关于切片和数组的内容,可以参考 Andrew Gerrand 的一篇博客文章,介绍了Go 切片的使用和内部原理。)

英文:

The question as phrased actually references Arrays and Slices. The question text is about an array and the code is illustrating using a slice. Therefore there two questions are implied; pass a single item slice, and pass a single item array.

An array: var a [1]string
A slice: var s []string

Passing a single item slice to the function:

func SliceFunc( slc []string) {
	fmt.Println(slc)
}

func main() {
    a := "stringy"
    SliceFunc( []string{a} )
    // or an actual array to the same function
    b := [...]string { "thingy" }
    SliceFunc( []string{b[0] )	
}

Passing a single item array to the function.

Here there is an issue, as an array has a fixed length and as a parameter to a function it cannot accept different length arrays so we are left with working function which has limited flexibility:

func ArrayFunc( arr [1]string) {
	fmt.Println(slc)
}

func main() {
    var a [1]string
    a[0] = "stringy"
    ArrayFunc( a )	
}

It seems that as a generalization sticking to slices is a more flexible solution.

(If you would like more on Slices and Arrays here one blog by Andrew Gerrand covering go slices usage and internals.)

答案4

得分: 0

你可以使用appendmake

package main
import "fmt"

func main() {
   {
      var a []string
      a = append(a, "north")
      fmt.Println(a)
   }
   {
      a := make([]string, 1)
      a[0] = "north"
      fmt.Println(a)
   }
}

https://golang.org/pkg/builtin

英文:

You can utilize append or make:

package main
import "fmt"

func main() {
   {
      var a []string
      a = append(a, "north")
      fmt.Println(a)
   }
   {
      a := make([]string, 1)
      a[0] = "north"
      fmt.Println(a)
   }
}

https://golang.org/pkg/builtin

huangapple
  • 本文由 发表于 2013年9月20日 07:54:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/18906817.html
匿名

发表评论

匿名网友

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

确定