Sort a Slice of Structs by a Field in Go
Sorting a slice in golang is easy and the “sort” package is your friend. Sort package has a Slice() method: In this code example, we… Read More »Sort a Slice of Structs by a Field in Go
Sorting a slice in golang is easy and the “sort” package is your friend. Sort package has a Slice() method: In this code example, we… Read More »Sort a Slice of Structs by a Field in Go
If you want to check the equality of two slices and the order is important then this snippet will save your time:
Some functions need a slice ([]string, []int, …) but you have an array (for example: [3]string, [10]int). Converting an array is as easy as calling… Read More »How to Convert an Array Into a Slice in Go
The simplest way to check if two structs are equal is using the DeepEqual method from the reflect package: See this example which shows the… Read More »Compare if Slices Are Equal in Golang (Struct, Map, Interface, …)
If you are interested in the order of your slice and want to remove some of them in an efficient way this snippet is for… Read More »Efficiently Keep Order When Removing Elements From a Slice in Golang
Get the index of the last item in Golang by using len() method: The slice index starts from 0 and you can access the last… Read More »Go: Remove the Last Element From a Slice
You are writing a piece of code that it is important for you to have the best performance, if you are working with the slices… Read More »Golang: Efficiently Remove an Element From a Slice (No Order)
When you want to check if a slice is empty simply check the length of the slice with len() method: You can use len() on… Read More »Golang: Check If slice Is Empty
Simplest way to convert a string to a slice of bytes: A set of examples to convert string to []byte:
Append is a basic function you need to learn when you are working with slices in golang: This example shows you three ways of appending… Read More »Golang Slice Append: One or Multiple Items