Strings are a basic data structure, and every language has a number of predefined functions for manipulating strings. In Go, these are gathered in a package, strings. We’ll discuss below some very useful functions one by one.

Prefixes and suffixes

HasPrefix tests whether the string s begins with a prefix prefix:

strings.HasPrefix(s, prefix string) bool

HasSuffix tests whether the string s ends with a suffix suffix:

strings.HasSuffix(s, suffix string) bool

The following program implements these functions:

Get hands-on with 1200+ tech skills courses.