All Go Rust Python PHP JavaScript
Chrome Dev Summit to secure your spot in workshops, office hours and learning lounges!

How to Compare if two Maps are Equal in Golang

Testing the equivalence of maps in Go. reflect.DeepEqual Function It first checks if both maps are nil, then it checks if they have the same length before finally checking to see if they have the same set of (key, value) pairs. package main impo...
Patcher56

How to Format a Currency with Commas in Golang

Formatting float to currency string in Go. Using localized formatting Use golang.org/x/text/message to print using localized formatting for any language in the Unicode CLDR. Make a main.go file containing the following: package main import ( "...
Unused

How to sort a vector in Rust

In Rust, there are 2 ways to sort a vector. Using sort Method The sort(&mut self) method sorts the slice. This sort is stable (i.e., does not reorder equal elements) and O(n * log(n)) worst-case. For example, fn main() { let mut v = [3, -5, ...
Tomoki

How to Check If two Strings are Equal in a Case-Insensitive Manner in Go

In Golang, using the strings.EqualFold function is the easiest way to check if two strings are equal in a case-insensitive manner Using strings.EqualFold Function The strings.EqualFold() function reports whether s and t, interpreted as UTF-8 strings...
Unused

How to Delete a List Element by Value in Python

In Python, there are 3 ways to delete a list element by value. Using list.remove Function The list.remove(x) function removes the first item from the list whose value is equal to x. It raises a ValueError if there is no such item. See the following...
Unused

How to Check for NaN Values in Python

In Python, there are 2 ways to check for NaN values. Using math.isnan Method The math.isnan(x) method returns True if x is a NaN (not a number), and False otherwise. For example, #!/usr/bin/python3 # Import module import math f = float('nan') ...
ada

How to Check the Equality of two Slices in Go

In Golang, there are 3 ways to check the equality of two slices. Using bytes.Equal Function Equal reports whether a and b are the same length and contain the same bytes. A nil argument is equivalent to an empty slice. For example, package main...
aweis

How To Split a list into equally sized chunks in Python

In Python, there are 3 ways to split a list into equally sized chunks. Using List Comprehension List comprehensions provide a concise way to create lists. The following example: #!/usr/bin/python3 # -*- coding: utf8 -*- # initialize lst = ['a'] *...
Patcher56

How to split a slice into equally sized chunks in Go

Slice chunking in Go.Using for Loop The easiest method involves iterating over the slice and incrementing by the chunk size. An implementation is shown in the section below. package main import "fmt" func chunkSlice(slice []int, chunkSize int) ([]...
Unused

How to generate a random string in Go

Creating Random Strings of a fixed length in Go. Using rand.Int63 Function Int63 returns a non-negative pseudo-random 63-bit integer as an int64 from the default Source. package main import ( "fmt" "math/rand" "time" ) const letters = "a...
ada