Lesson 3
Navigating Go's Built-in Functions and Packages
Introduction to Built-in Functions in Go

Welcome, intrepid coder! Are you ready to embark on a journey into the realm of Go's built-in functions? These robust tools can enhance the efficiency of your code, saving time and effort. Our goal is to familiarize ourselves with some of the most straightforward built-in functions in Go. Let's begin this adventure!

Much like the onboard computing systems that guide astronauts, Go's built-in functions serve as readily available tools for navigating various coding tasks at any given moment. They act as our guiding stars through the immense cosmos of programming.

Exploring Simple Go Built-in Functions: len()

Let's chart a course through Go's built-in functions:

The len() function gives the number of elements in a slice, string, array, or the keys in a map, much like counting the stars in a constellation:

Go
1package main 2import "fmt" 3 4func main() { 5 stars := []string{"star1", "star2", "star3", "star4"} 6 fmt.Println(len(stars)) // Output: 4 7 8 starMap := map[string]int{ 9 "star1": 1, 10 "star2": 2, 11 "star3": 3, 12 "star4": 4, 13 } 14 fmt.Println(len(starMap)) // Output: 4 15 16 starStr := "star1, star2, star3, star4" 17 fmt.Println(len(starStr)) // Output: 26 18}
Absolute Value: Abs()

Housed within the math package, the Abs() function gives the absolute value of a float64 number. It's much like a navigational device measuring a spaceship's distance from a specific point in space in light-years:

Go
1package main 2import ( 3 "fmt" 4 "math" 5) 6 7func main() { 8 fmt.Println(math.Abs(-10.00)) // Output: 10 9 fmt.Println(math.Abs(7.00)) // Output: 7 10}

Note that we import "math" here. We will need to import the "math" package for a wide range of math functions in Go, including the functions from the next section.

Float Numbers Rounding: Round(), Floor() and Ceil()

The Round(), Floor(), and Ceil() functions from the math package demonstrate how rounding a number to the nearest integer is achieved in Go. These functions operate similarly to rounding off estimated distances or times:

Go
1package main 2import ( 3 "fmt" 4 "math" 5) 6 7func main() { 8 fmt.Println(math.Round(10.675)) // Output: 11 9 fmt.Println(math.Floor(10.365)) // Output: 10 10 fmt.Println(math.Ceil(7.5)) // Output: 8 11}

Round() rounds the number according to math rules: any number is rounded to the nearest integer, and 0.5 is rounded up. Floor() rounds down to the nearest integer and Ceil() rounds up to the nearest integer.

Sorting Elements in Go with the `sort` package

Sorting elements in Go can be done using functions in the sort package:

Go
1package main 2import ( 3 "fmt" 4 "sort" 5) 6 7func main() { 8 artifacts := []int{50, 100, 25, 75} 9 sort.Ints(artifacts) 10 fmt.Println(artifacts) // Output: [25, 50, 75, 100] 11 12 // To sort in reverse order 13 sort.Sort(sort.Reverse(sort.IntSlice(artifacts))) 14 fmt.Println(artifacts) // Output: [100, 75, 50, 25] 15}

Let’s take a deeper look at line 13, which sorts artifacts in descending order.

  1. sort.IntSlice(artifacts) - This converts artifacts into a sort interface for slices of int.
  2. sort.Reverse - This creates a reverse order for the sort interface.
  3. sort.Sort - This sorts the elements in the now reversed sort interface.

So, the overall effect of this line is to sort the integer slice artifacts in descending order. While it can sound like a lot of details, it shows how the built-in function can help us solve a complex task in just one line of code. Imagine if we were to code the sorting from scratch! It would require much more of time and effort.

Lesson Summary

You have now learned to use Go's built-in functions—len(), math.Abs(), math.Round(), math.Floor(), math.Ceil()—as well as methods in the sort package. Continue to practice with these functions to hone your programming skills further. Now, you are ready for exercises designed to polish these skills. Keep exploring!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.