substring in golang like python

To perform substring operations in Go, you can use the substring package from the standard library. Here are the steps to perform substring operations in Go:

  1. Import the necessary package: import "strings"

  2. Declare a variable to store the input string: input := "Hello, World!"

  3. Use the strings.Index() function to find the starting index of the substring: start := strings.Index(input, "World")

This function returns the index of the first occurrence of the substring within the input string. In this example, the value of start will be 7.

  1. Use the strings.LastIndex() function to find the ending index of the substring: end := strings.LastIndex(input, "World") + len("World")

This function returns the index of the last occurrence of the substring within the input string. By adding the length of the substring, we get the index of the character just after the end of the substring. In this example, the value of end will be 12.

  1. Use the input[start:end] syntax to extract the substring: substring := input[start:end]

This syntax allows you to extract the substring from the input string using the start and end indices. In this example, the value of substring will be "World".

By following these steps, you can perform substring operations in Go similar to how you would in Python.