Book Image

Hands-On Go Programming

By : Tarik Guney
Book Image

Hands-On Go Programming

By: Tarik Guney

Overview of this book

<p>With its C-like speed, simplicity, and power for a growing number of system-level programming domains, Go has become increasingly popular among programmers. Hands-On Go Programming teaches you the Go programming by solving commonly faced problems with the help of recipes. You will start by installing Go binaries and get familiar with the tools used for developing an application. Once you have understood these tasks, you will be able to manipulate strings and use them in built-in function constructs to create a complex value from two floating-point values. You will discover how to perform an arithmetic operation date and time, along with parsing them from string values. In addition to this, you will cover concurrency in Go, performing various web programming tasks, implementing system programming, reading and writing files, and honing many fundamental Go programming skills such as proper error handling and logging, among others. Whether you are an expert programmer or newbie, this book helps you understand how various answers are programmed in the Go language.</p>
Table of Contents (18 chapters)
Title Page
Copyright and Credits
Packt Upsell
Contributor
Preface
Index

Reverting an array


In this section, we are going to learn how to reverse sort an array. We'll have a variable that holds a slice of numbers. Since you are now familiar with the sort package in Go, you'll know that the sort package provides a lot of functionality that we can use to sort arrays and slices. If you look at the sort package, you'll see many types and functions. 

 

Now, we need the sort function and it accepts an interface, and this interface is defined in the sort package; therefore, we can call it the Sort interface. We are going to convert our slice of numbers into an interface. Check out the following code:

package main
import (
  "sort"
  "fmt"
)
func main() {
  numbers := []int{1, 5, 3, 6, 2, 10, 8}
  tobeSorted := sort.IntSlice(numbers)
  sort.Sort(tobeSorted)
  fmt.Println(tobeSorted)
}

This code will give you the following output:

If you look at the output, you'll see that we have sorted the numbers in ascending order. What if we want to sort them in descending order? To be...