ProgramGuru

String Comparison

Start with String Comparison

Go strings can be compared with ==, != and ordering operators. Equality checks whether their byte sequences match.

What you’ll learn

  • Explain the main purpose of string comparison.
  • Read and run the complete Go example in this lesson.
  • Apply the concept and recognize common mistakes.

Understand the idea

Use == for exact equality. The ordering operators < and > compare strings lexicographically by their bytes; this is not language-aware dictionary ordering. Uppercase and lowercase letters have different byte values.

For case-insensitive Unicode case folding, strings.EqualFold is often helpful. It does not mean accents or visually similar but differently normalized Unicode sequences will always compare equal. Choose comparison rules that match the application’s actual requirements.

Visualize how it works

Follow this diagram before executing the example. Check where a value or decision changes what runs next.

Visual explanation for string comparisonA condition branches into true or false paths before continuing.Compare Go and goTRUEFALSEExact equality: falseEqualFold: trueChoose the matching rule
Trace control flow in the order shown, then compare it with the program.

Run a complete Go example

This example is a complete program. Save it as main.go or paste it into the Go Playground.

Go code

package main

import (
	"fmt"
	"strings"
)

func main() {
	first := "Go"
	second := "go"
	fmt.Println("Exact equal:", first == second)
	fmt.Println("EqualFold:", strings.EqualFold(first, second))
	fmt.Println("Alphabetical bytes:", "apple" < "banana")
}

Output

Exact equal: false
EqualFold: true
Alphabetical bytes: true

Trace what the program does

The first comparison is false because G and g are different bytes. EqualFold treats these words as equal under Unicode case folding. The bytewise lexicographic comparison of apple and banana is true.

Avoid common mistakes

  • == is case-sensitive and compares actual byte sequences.
  • < is not a locale-aware alphabetical sorting rule for natural language.

Try it yourself

Compare "Go" and "GO" with == and strings.EqualFold, then explain why the results differ.

Tip: Edit the example above, predict the output, and run the modified code to test your reasoning.

Check your understanding

Which function performs Unicode case-folded equality?
Choose one answer. You can try again.

Answer and explanation

strings.EqualFold

Correct. The first comparison is false because G and g are different bytes.

What you should remember

You have practiced String Comparison in a small, self-contained program. The important habit is to predict what runs, inspect the actual output and distinguish the language rule from the particular example.

Before continuing, make sure you can explain the program to someone who has not seen it, including the line that changes its result.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: String Concatenation →