To get all possible substrings of a string in Go, you can use nested loops to iterate through each combination of start and end indices.
In this example,
str
with a value of 'Hello'
.substrings
to store all substrings.substrings
slice.substrings
slice.package main
import (
"fmt"
)
func main() {
str := "Hello"
substrings := []string{}
for i := 0; i < len(str); i++ {
for j := i + 1; j <= len(str); j++ {
substrings = append(substrings, str[i:j])
}
}
fmt.Println("All substrings:", substrings)
}
All substrings: [H He Hel Hell Hello e el ell ello l ll llo l lo o]
In this tutorial, we learned How to get all the possible Substrings of a String in Go language with well detailed examples.