GeekBits

Difference Between []string and ...string In Golang

In this guide, we are going to explore what these two way of strings in Go. We will cover how strings work, and the differences between the two.

3 min read
Difference Between []string and ...string In Golang

Like in most programming, the string data type is a fundamental type in Go. It allows us to represent a sequence of characters which is the basis of text data.

However, unlike most programming languages we know, Go provides two different ways of working with string values. The first is using an array format []string while the other uses elipsis format as ...string.

string in Go

In Go, a string allows us to store a sequence of characters, where each character is represented as Unicode code point.

As we mentioned, the string is the fundamental data type for storing text data and the associated methods and techniques of manipulating text data.

In Go, strings are immutable data type. As a resultl, once we create it, we cannot change it during its lifetime.

This means that if there is any operation that appears to change the string, in actuality, it is creating a new string with the new changes but not modifying the underlying reference.

How a String Works

Let us go deep into the world of Go and try to cover how a string actually works in a more technical capacity.

Internally, a string in Go is represented as a read-only slice of bytes []byte. As a result, this representation allows the Go compiler to efficiently wokr with strings by employing standard slice operations. This is especially useful for extracting substrings, etc.

A Go string consists of the following parts:

  • Data Pointer - A pointer to the underlying byte slice that contains the actual string data.
  • Length - The length of the string, indicating the number of characters in the string.
  • Capacity - The capacity of the underlying byte slice, which can be greater than or equal to the length of the string.

Here is a basic visual representation of the string.

+------------------+
|   Data Pointer  | --> Points to underlying []byte
+------------------+
|     Length       | --> Number of characters in the string
+------------------+
|     Capacity     | --> Capacity of the underlying []byte
+------------------+

Let us explore the differences between []string and ...string.

[]string vs. ...string

[]string

In Go, the []string is a slice of strings. It is a data structure that can hold multiple strings as elements within a dynamic array.

The characteristics of this type include:

  1. Dynamic Length - A []string can hold an arbitrary number of strings, and its length can be changed dynamically using the append function.
  2. Indexed Access - You can access individual strings in a []string using indexing, just like you would with a regular slice.

An example is as shown:

package main
import "fmt"
func main() {
    servers := []string{"192.168.100.12", "192.165.122", "192.23.34.222"}
    fmt.Println(servers[0])
}

In the example above, we create a string using the []string and add the specified number of string values. We then use the indexing notation to access the first element.

go run strings.go

Output:

192.168.100.12

...string

In Go, the ...string represents an ellipsis type. This type represents a variable number of string arguments that we can pass to a function.

Hence, we often used it for variadic function parameters. Some common characteristics include:

  • Variadic Function Parameters - we use the...string in function declarations to accept a variable number of string arguments.
  • No Direct Indexing - We cannot directly access individual elements of ...string since it represents a variable number of arguments.

An example is as shown:

package main
import "fmt"
func printAddresses(servers ...string) {
    for _, address := range servers {
        fmt.Println(address)
    }
}
func main() {
    printAddresses("192.168.100.12", "192.165.122", "192.23.34.222")
}

In this case, we use a for loop inside the printAddresses() function and pass any number of string using the ...string format.

Output:

192.168.100.12
192.165.122
192.23.34.222

Main Differences

The following are the major differences between the two.

  • Type - []string is a slice of strings, while ...string is an ellipsis type for variadic function parameters.
  • Usage - []string is used to create and work with dynamic arrays of strings, whereas ...string is used to accept a variable number of string arguments in functions.
  • Indexing - []string allows direct indexing and individual element access, whereas ...string does not support direct indexing.
  • Mutability - []string is mutable, meaning you can modify its elements, while ...string is typically used for read-only access within functions.

Conclusion

In this post we learned about the difference between []string and ...string . In Go, []string is used for managing collections of strings, while ...string is used for receiving a variable number of string arguments in functions.

Sign up for our newsletter

Don't miss anything. Get all the latest posts delivered to your inbox. No spam!