liveBook · Manning

Comparable

As explained in Go in Action, Second Edition

comparable is a predeclared Go interface constraint satisfied by types whose values support equality and inequality comparisons.

A generic collection needs a constraint when its operations compare stored values. In Go, `comparable` restricts a type parameter to types that support `==` and `!=`. That lets equality-dependent operations be checked at compile time. section 6.1

An equality-aware linked list

Declare `LinkedList[T comparable]` when the list needs to find or remove an item by comparing it with stored values. Its constructor carries the same constraint: `func NewLinkedList[T comparable]() *LinkedList[T]`. Methods such as `Contains` and `Remove` can then test `curr.value == item`. section 6.1 section 6.2 section 6.3

Listing 6.14 Implementing part of the interface
func (l *LinkedList[T]) Remove(item T) bool {         #1
	var prev *node[T]
	curr := l.head
	for curr != nil {
		if curr.value == item {                        #2
			if prev == nil {
				l.head = curr.next                    #3
			} else {
				prev.next = curr.next                 #4
			}
			l.size--                                  #5
			return true
		}
		prev = curr
		curr = curr.next
	}
	return false
}

func (l *LinkedList[T]) Contains(item T) bool {       #6
	curr := l.head
	for curr != nil {
		if curr.value == item {                        #7
			return true
		}
		curr = curr.next
	}
	return false
}

func (l *LinkedList[T]) Size() int {                  #8
	return l.size
}

func (l *LinkedList[T]) Clear() {                     #9
	l.head = nil
	l.size = 0
}

The `Remove` and `Contains` methods show where the constraint pays off: both traverse the list and compare each stored value with the requested item. section 6.3

The broader `List[T any]` interface describes operations without requiring equality. A concrete `LinkedList[T comparable]` can implement that interface while accepting only element types suitable for its equality-based methods. Choose the constraint for the operations an implementation performs, rather than making every collection require comparison. section 6.1 section 6.3

How Go in Action, Second Edition covers it

How deep it goes
Works through a constrained type, constructor, and equality-based methods rather than comparison mechanics.
Examples
Uses a generic LinkedList constructor and Remove and Contains methods that compare stored values with an item.
What it assumes
Builds on type parameters and generic interfaces while introducing comparable as a more restrictive constraint than any.
What you can do after
Build it from scratch: declare a constrained linked list and implement equality-based collection operations.
How it connects
Connects type parameters, generic interfaces, linked lists, and compile-time type safety.

Also explained in

Compare how 3 books explain comparable →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Go in Action, Second Edition. The section links point to the parts of the book it draws on.