Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Exercise: Create a list of words from sentences

  • Given a list of strings with words separated by spaces, create a single list of all the words.
package main

import (
	"fmt"
)

func main() {
	// source:
	lines := []string{
		"grape banana mango",
		"nut orange peach",
		"apple nut banana apple mango",
	}
	fmt.Println(lines)

	// result:
	fruits := []string{"grape", "banana", "mango", "nut", "orange", "peach", "apple", "nut", "banana", "apple", "mango"}
	fmt.Println(fruits)
	for _, fruit := range fruits {
		fmt.Println(fruit)
	}
}