Go: checking if a string contains a substring — complete guide

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

How to check if a string contains a substring in Go? From beginner to pro

\n\n

Have you ever been writing Go and suddenly froze in front of a simple question: "Does this string contain the part I need?" If so, you are not alone. On Stack Overflow, this question has garnered thousands of views, and for good reason. Go has several elegant ways to solve this problem, but which one to choose? Let's figure it out once and for all.

\n\n

In this article, we will not just show you the strings.Contains function. We will teach you to think like a Go developer: choose the optimal tool for each situation, avoid pitfalls, and write code that is fast and predictable.

\n\n

Table of Contents

\n\n\n

1. strings.Contains — your first friend

\n\n

Let's start with the simplest and most popular method. The strings.Contains function from the Go standard library is what you need in 80% of cases. It returns true if the string contains the specified substring, and false otherwise.

\n\n

Here's what it looks like in real code:

\n\n
package main\n\nimport (\n    "fmt"\n    "strings"\n)\n\nfunc main() {\n    str := "Hello, Go world!"\n    substr := "world"\n\n    if strings.Contains(str, substr) {\n        fmt.Println("The string contains the substring:", substr)\n    } else {\n        fmt.Println("Substring not found")\n    }\n}\n
\n\n

Output:

\n
The string contains the substring: world
\n\n

Simple, right? The function is case-sensitive: strings.Contains("Go", "go") will return false. If you need to ignore case, you'll have to use strings.ToLower or strings.EqualFold.

\n\n

Example with case-insensitive search

\n\n
str := "Golang is awesome"\nsubstr := "golang"\n\nif strings.Contains(strings.ToLower(str), strings.ToLower(substr)) {\n    fmt.Println("Found (case-insensitive)!")\n}\n
\n\n

When to use: when you need a simple binary answer — "yes" or "no". Without unnecessary details.

\n\n

2. strings.Index — when you need to know the position

\n\n

Sometimes it's not enough to know that a substring exists. You want to understand where exactly it is located. For example, you are parsing a log and want to extract part of a string after a certain marker. This is where strings.Index comes to the rescue.

\n\n

This function returns the index of the first occurrence of the substring (starting from 0) or -1 if the substring is not found.

\n\n
str := "Error: file not found in /var/log"\nsubstr := "file"\n\npos := strings.Index(str, substr)\nif pos != -1 {\n    fmt.Printf("Substring found at position %d\\n", pos)\n    fmt.Printf("Context: %s\\n", str[pos:])\n} else {\n    fmt.Println("Substring not found")\n}\n
\n\n

Output:

\n
Substring found at position 7\nContext: file not found in /var/log
\n\n

There are other variations:

\n
    \n
  • strings.LastIndex — searches for the last occurrence
  • \n
  • strings.IndexAny — searches for the first occurrence of any character from a set
  • \n
  • strings.IndexRune — searches for a specific Unicode character
  • \n
\n\n
// Example with LastIndex\nstr := "a-b-c-d"\nlastDash := strings.LastIndex(str, "-")\nfmt.Println("Last dash at position:", lastDash) // 5\n\n// Example with IndexAny\nstr2 := "hello world"\npos := strings.IndexAny(str2, "aeiou")\nfmt.Println("First vowel at po                

Blogs

Book Recommendations