My new course Building a Production-Ready Service in Go is almost complete - try it out and let me know what you think!

Checking if a string contains a sub-string in Go Image Checking if a string contains a sub-string in Go

In this snippet, we are going to see how we can test if a string contains another string in Go.

To do this we’ll be using the standard library strings package as it contains an incredibly handy function Contains which we can pass 2 arguments into. The first argument is our full string, the second is the string we want to check for.

In this example, we have our originalString which we test to see if it contains the string our.

We then use the same function to check a negative case and see if meh exists in the second string.

Further Reading:

If you liked this snippet, you make also like some of the other content on this site: