O'Reilly logo

Go Standard Library Cookbook by Radomir Sohlich

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

How it works...

The sample code shows both ways of how to comfortably sort a slice with the help of the sort package. The first approach is more ad hoc and it uses the Slice function of the sort package. The Slice function consumes the slice to be sorted and the so-called less function, which defines whether the element i should be sorted before element j

The second approach requires more code and planning ahead. It leverages the Interface interface of the sort package. The interface acts as a representative of the data and requires it to implement essential methods on sorted data: Len (defines the amount of data), Less (less function), Swap (method called to swap the elements). If the data value implements this interface, then the Sort ...

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