August 03, 2025
Go Contains function for string search
You can search a character or even a string from a string through Contains function of string package in Go. Contains function required two string parameters, first is string, second sub string which needs to be searched. The Contains method returns boolean values if string is found then it returns true else it returns false. The example of find method is mentioned below:
Code
package main
import (
"fmt"
"strings"
)
func main() {
weekDaysString := "Sunday, Monday, Tuesday, Wednesday, Thursday"
result := strint.Contains(weekDaysString, "Tuesday")
// Check if "Go" is in the text
if result {
fmt.Println("Your string is found!")
} else {
fmt.Println("Your string is not found!")
}
}
In above mentioned example we have a string variable with the name of weekDaysString and we make a search with string.Contains function and try to find the 'Tuesday' string, and store the result in result variable. We have also write a if conditional statement which shows a message that "Your string is found" if string found else if string does not found then shows a message that "Your string is not found".