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 Check if a Slice Contains an Element in Go

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 _, ...
ada

How to Check String is In Json Format in Go

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 ...
pooriabt

How to Delete a Character from a String in Python

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...
Unused

How to Get a SHA256 Hash from a String in Go

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...
ada

How to Get a MD5 Hash from a String in Go

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...
aweis

How to Check if IP Address is in Private Network Space in Go

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...
aweis

How to check if a string contains a specific word in PHP

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...
ada

How to Check if a String Contains Whitespace in Rust

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() { ...
Unused

How to Get the Psition of a Character in Python

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...
aweis

How to Check if Directory on Path is Empty in Go

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 ...
Sambhav Khandelwal

How to Check if a File is a Valid Image in Go

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...
pooriabt

How to Set Environment Variables in Python

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 ...
Sambhav Khandelwal

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