Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create an array of one item in Go?

Tags:

go

Let's say a function takes an array 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 an array?

like image 252
Kevin Burke Avatar asked Sep 19 '13 23:09

Kevin Burke


People also ask

How do you do an array in Go?

In Go language, arrays are mutable, so that you can use array[index] syntax to the left-hand side of the assignment to set the elements of the array at the given index. You can access the elements of the array by using the index value or by using for loop. In Go language, the array type is one-dimensional.

How do you initialize an array in Go?

You can initialize an array with pre-defined values using an array literal. An array literal have the number of elements it will hold in square brackets, followed by the type of its elements. This is followed by a list of initial values separated by commas of each element inside the curly braces.

How do I add elements to an array in Go?

The append function is a built-in function which appends elements to the end of a slice. It takes care of allocating the underlying arrays should they need to be resized to accommodate any new elements and then returns the new slice.

How do I create an array of slices in Golang?

creating a slice using make func make([]T, len, cap) []T can be used to create a slice by passing the type, length and capacity. The capacity parameter is optional and defaults to the length. The make function creates an array and returns a slice reference to it.


3 Answers

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.

like image 149
Mostafa Avatar answered Oct 08 '22 01:10

Mostafa


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);
like image 24
Kevin Burke Avatar answered Oct 08 '22 02:10

Kevin Burke


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.)

like image 41
miltonb Avatar answered Oct 08 '22 03:10

miltonb