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...
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 (
"...
In Golang, there are 2 ways to check if a slice contains an element.
Using contains Function
The following example should cover whatever you are trying to do:
package main
import "fmt"
func Contains[E comparable](s []E, v E) bool {
for _, ...
In Golang, there are 2 ways to check string is in json format.
Using Unmarshal Function
The following example should cover whatever you are trying to do:
package main
import (
"encoding/json"
"fmt"
)
func isJson(s string) bool {
var js ...
In Python, there are 2 ways to delete a character from a string.
Using replace Method
The str.replace(old, new[, count]) method returns a copy of the string with all occurrences of substring old replaced by new. If the optional argument count is giv...
In Golang, using the Sum256 function is the easiest way to get a SHA256 hash from a string.Go implements several hash functions in various crypto/* packages.
Using Sum256 Function
The sha256.Sum256() function returns the SHA256 checksum of the data...
In Golang, using the Sum function is the easiest way to get a MD5 hash from a string.
Using Sum Function
The md5.Sum() function returns the MD5 checksum of the data.
The following example should cover whatever you are trying to do:
package mai...
In Golang, using the IsPrivate function is the easiest way to check if IP address is in private network space.
Using IsPrivate Function
The IsPrivate() function reports whether ip is a private address, according to RFC 1918 (IPv4 addresses) and RFC...
In PHP, there are 2 ways to check if a string contains a specific word.
Using strpos Function
The strpos(string $haystack, string $needle, int $offset = 0): int|false function finds the numeric position of the first occurrence of needle in the hayst...
In Rust, there are 2 ways to check if a string contains whitespace.
Using char::is_whitespace
char::is_whitespace returns true if the character has the Unicode White_Space property.
You can pass char::is_whitespace to .contains():
fn main() {
...
In Python, there are 3 ways to get the position of a character.
Using find Method
The str.find(sub[, start[, end]]) method returns the lowest index in the string where substring sub is found within the slice s[start:end]. Optional arguments start an...
In Golang, using the Readdirnames function is the easiest way to check if directory on path is empty.
Using Readdirnames Function
The file.Readdirnames(n int) function reads the contents of the directory associated with file and returns a slice of ...
In Golang, there are 2 ways to check if a file is a valid image.
Using DetectContentType Function
The http.DetectContentType() function implements the algorithm described at https://mimesniff.spec.whatwg.org/ to determine the Content-Type of the gi...
In Python, using the os.environ variable is the easiest way to set environment variables
Using os.environ Variable
os.environ behaves like a python dictionary, so all the common dictionary operations can be performed. In addition to the get and set ...
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...