Go split function

split() function is one of the most important Go language built in function of string manipulation. Sometimes it happens that you want to convert a string's element into an array so for the same you can use split() function in Go language. split() function requires three string arguments first is string on which we need perform operation, second is separator argument, it is called on a string object from which you will create an array, third is a limit for the number of parts. The split() function divide the string in small chunk through the separator string and write in an array element. Every time split() function found the separator string it creates an new array element. The example of split() function is mentioned below: Code


weekDaysArray = weekDaysString.split(", ")
print(weekDaysArray)

package main

import (
	"fmt"
	"strings"
)

func main() {
	weekDaysString := "Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday"

	separator := ","

	array_element := strings.Split(weekDaysString, separator)

	fmt.Println(parts)
}
In above mentioned example we have a string variable with the name of weekDaysString which have all the week days name in comma separated format. In the next line we have separator variable. In the next line we have use split function on weekDaysString string variable and provide separator string as comma(,) then split function returns an array of all the week days and we store it in weekDaysArray and print it on the screen through print function.