Merge sort is a recursive sorting algorithm and, luckily for us, it’s quite a bit faster than bubble sort. Merge sort is a divide and conquer algorithm. Divide Divide the input slice into two (equal) halves Recursively sort the two halves Conquer Merge the two halves to form a sorted array Full example of the merge sort algorithm Merge sort actually has two functions involved, the recursive mergeSort function, and the merge function.
Bubble sort is named for the way elements “bubble up” to the top of the list. Bubble sort repeatedly steps through a slice and compares adjacent elements, swapping them if they are out of order. It continues to loop over the slice until the whole list is completely sorted. Full example of the bubble sort algorithm func bubbleSort(input []int) []int { swapped := true for swapped { swapped = false for i := 1; i < len(input); i++ { if input[i-1] > input[i] { input[i], input[i-1] = input[i-1], input[i] swapped = true } } } return input } Using the algorithm in code func main() { unsorted := []int{10, 6, 2, 1, 5, 8, 3, 4, 7, 9} sorted := bubbleSort(unsortedInput) // sorted = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] } Learn Go by writing Go code I'm a senior engineer learning Go, and the pace of Boot.
What is the “defer” keyword in Go? In the Go programming language, defer is a keyword that allows developers to delay the execution of a function until the current function returns. What throws some people off is that the deferred function’s arguments are evaluated immediately, but the function itself doesn’t fire until the wrapping function exits. Simple defer example - hello world func main() { defer fmt.Println("world") // deferred until main() exits fmt.
Keeping track of time in code has long been every developer’s nightmare. While no language or package manages time perfectly, I think Golang does a pretty good job out-of-the-box. This full tutorial should answer ~90% of the questions you’ll have about time management in Go. Overview - How dates and times are stored in Go The first thing to know is that you probably don’t need any third-party packages to manage times and dates in Go.
The Go standard library makes concatenating strings easy. Concatenation is just a fancy word for adding strings together to make a larger string. For example, if we concatenate "hello", " " and "world" we’d get "hello world". The built-in [fmt.Sprintf](https://golang.org/pkg/fmt/#Sprintf) function takes a format and a variadic list of interfaces as input. func Sprintf(format string, a ...interface{}) string The formatting option lets us template out how the final string will look, then we can add inputs that will be interpolated into the string.
These two coding languages duke it out - but who’s the winner? In a world where the ability to write any code at all is a tremendous advantage, often the biggest problem coders face is knowing which language to start learning, rather than whether to learn one at all. There are different languages for just about every purpose you could think of. Of those popular coding languages, programmers often face an intense battle of Golang vs Python.
As a language designed for the web, Go provides extensive support for working with JSON data. JSON (JavaScript Object Notation) is an incredibly popular data exchange format whose syntax resembles simple JavaScript objects. It’s one of the most common ways for applications to communicate on the modern web. Encoding and decoding with struct tags Go takes a unique approach for working with JSON data. The best way to think about JSON data in Go is as an encoded struct.
Go has a powerful standard library that makes string manipulation easy right out of the box. One of the functions I use most often is the strings package’s Replace() function. strings.Replace() returns a copy of its input string after replacing all instances of the given substring with a new one. strings.Replace() signature func Replace(s, old, new string, n int) string Notes s is the original string that contains the substrings to be replaced.
An enum (short for enumerator), is a set of named constant values. An enum is a powerful tool that allows developers to create complex sets of constants that have useful names and yet simple and unique values. Example of an idiomatic enum Within a constant declaration, the iota keyword creates enums as successive untyped integer constants. type BodyPart int const ( Head BodyPart = iota // Head = 0 Shoulder // Shoulder = 1 Knee // Knee = 2 Toe // Toe = 3 ) Why should you use enums?
I can’t begin to tell you how often I split strings in Go. More often than not I’m just parsing a comma-separated list from an environment variable, and Go’s standard library gives us some great tools for that kind of manipulation. Split by commas or other delimiters strings.Split() Go’s rich standard library makes it easy to split a string into a slice. 99% of the time you need to split strings in Go, you’ll want the strings package’s strings.