The Go Cookbook

Maintained by SuperOrbital.

A community built and contributed collection of practical recipes for real world Golang development.

View project on GitHub

Turning an Array into a Sentence

How can I convert an array of words into a readable sentence of options?

If you’re content with joining each of the strings in a given array using a consistent separator, then strings.Join() is the tool for you. Join() takes an array of strings, and a final string that acts as the separator. If we pass in ", " as our separator, then we come pretty close to producing a sentence:

test_join.go
package main

import (
	"fmt"
	"strings"
)

func main() {
	a := []string{"this", "that", "the other"}
	fmt.Println(strings.Join(a, ", "))
}
$ go run test_join.go
this, that, the other

Sometimes this is good enough, but we often want the resulting string to read more like and English sentence, offering all or some of the options. To produce this, we can create a ToSentence method like such:

test_sentence.go
package main

import (
	"fmt"
	"strings"
)

func ToSentence(words []string, andOrOr string) string {
	l := len(words)
	wordsForSentence := make([]string, l)
	copy(wordsForSentence, words)
	wordsForSentence[l-1] = andOrOr + " " + wordsForSentence[l-1]
	return strings.Join(wordsForSentence, ", ")
}

func main() {
	a := []string{"this", "that", "the other"}
	fmt.Println(ToSentence(a, "or"))
	fmt.Println(ToSentence(a, "and"))
}
$ go run test_sentence.go
this, that, or the other
this, that, and the other

…which is much more friendly when presented to our users.