Finding the substring in a string

Create the main.go file with the following content:

package main

import (
	"fmt"
	"strings"
)

const refString = "Mary had a little lamb"

func main() {

	lookFor := "lamb"
	contain := strings.Contains(refString, lookFor)
	fmt.Printf("The \"%s\" contains \"%s\": %t \n", refString,
		lookFor, contain)

	lookFor = "wolf"
	contain = strings.Contains(refString, lookFor)
	fmt.Printf("The \"%s\" contains \"%s\": %t \n", refString,
		lookFor, contain)

	startsWith := "Mary"
	starts := strings.HasPrefix(refString, startsWith)
	fmt.Printf("The \"%s\" starts with \"%s\": %t \n", refString,
		startsWith, starts)

	endWith := "lamb"
	ends := strings.HasSuffix(refString, endWith)
	fmt.Printf("The \"%s\" ends with \"%s\": %t \n", refString,
		endWith, ends)

}

output:-

Biradars-MacBook-Air-4:Documents sangam$ cd golang-daily/
Biradars-MacBook-Air-4:golang-daily sangam$ go run main.go
The "Mary had a little lamb" contains "lamb": true 
The "Mary had a little lamb" contains "wolf": false 
The "Mary had a little lamb" starts with "Mary": true 
The "Mary had a little lamb" ends with "lamb": true 
Biradars-MacBook-Air-4:golang-daily sangam$ 

How it works…