In Golang, you can extract the middle portion of a string using string slicing. Here's an example:
package main
import (
"fmt"
)
func main() {
str := "Hello World!"
// Calculate the start and end indices of the middle portion
start := len(str)/2 - 1
end := start + 3
// Extract the middle portion using string slicing
middle := str[start:end]
fmt.Println(middle) // Output: o W
}
In the above example, we define a string str
containing "Hello World!".
We then calculate the indices of the middle portion by dividing the length of the string by 2 (len(str)/2
) and subtracting 1 to get the starting index. We add 3 to the starting index to get the end index.
Finally, we use string slicing to extract the middle portion by providing the calculated start and end indices. The extracted middle portion is assigned to the middle
variable.
Running the above code will output o W
, which is the middle portion of the string "Hello World!".