O'Reilly logo

Isomorphic Go by Kamesh Balasubramanian

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Implementing the sort interface for the product model

Before we get started, we will define a new type called Products (in the shared/models/product.go source file), which will be a slice of Product objects:

type Products []*Product

We will have the Products type implement the sort interface by defining the following methods:

func (p Products) Len() int { return len(p) }func (p Products) Less(i, j int) bool { return p[i].Price < p[j].Price }func (p Products) Swap(i, j int) { p[i], p[j] = p[j], p[i] }

By examining the Less method, you will be able to see that we will sort the products displayed on the product listing page by the product's price in ascending order (lowest to highest).

At the first glance we may presume that the products ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required