Solution: Create a list of words from sentences



examples/words-from-sentences/words_from_sentences.go
package main

import (
    "fmt"
    "strings"
)

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

    var fruits []string
    // Append words from each line
    for _, line := range lines {
        words := strings.Split(line, " ")
        fmt.Printf("fruits: %v, appending words: %v\n", fruits, words)
        fruits = append(fruits, words...)
    }

    // Print final result
    fmt.Println(fruits)
    for _, fruit := range fruits {
        fmt.Println(fruit)
    }
}

examples/words-from-sentences/words_from_sentences.out
[grape banana mango nut orange peach apple nut banana apple mango]
fruits: [], appending words: [grape banana mango]
fruits: [grape banana mango], appending words: [nut orange peach]
fruits: [grape banana mango nut orange peach], appending words: [apple nut banana apple mango]
[grape banana mango nut orange peach apple nut banana apple mango]
grape
banana
mango
nut
orange
peach
apple
nut
banana
apple
mango