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 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 Make a List of Alphabet Characters in Python

In Python, there are 2 ways to make a list of alphabet characters. Using string Module The string.ascii_lowercase is a pre-initialized string used as string constant. In Python, string ascii_lowercase will give the lowercase letters ‘abcdefghijklmno...
Patcher56

How to Convert all Strings in a List to Int in Python

In Python, there are 2 ways to convert all strings in a list to int. Using map Method You can convert all strings in a list to int using the map method. For example, #!/usr/bin/python3 a = ["1", "2", "4", "5"] b = list(map(int, a)) print(b) ...
Sambhav Khandelwal

How to Remove Invalid UTF-8 Characters from a String in Go

In Golang, there are 3 ways to remove invalid UTF-8 characters from a string. Using ToValidUTF8 Function The strings.ToValidUTF8() function returns a copy of the string s with each run of invalid UTF-8 byte sequences replaced by the replacement str...
pooriabt

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 Make First Letter of Words Uppercase in a String in Go

In Golang, using the cases package is the easiest way to make first letter of words uppercase in a string. Using cases Package install Use the following command to download the repository to the local file system. install go mod tidy ...
pooriabt

How to Count the Items in the Map in Go

In Golang, using the len function is the easiest way to count the items in the map structure. Using len Function The built-in function len take arguments of various types and return a result of type int. The implementation guarantees that the result...
Sambhav Khandelwal

How to Check If an Object Is of a Given Type in Python

In Python, there are 2 ways to check if an object is of a given type. Using isinstance Method The isinstance(object, classinfo) method returns True if the object argument is an instance of the classinfo argument, or of a (direct, indirect, or virtua...
Unused

How to Remove a Trailing Newline in Python

In Python, there are 4 ways to remove a trailing newline. Using rstrip Method The str.rstrip() method returns a copy of the string with trailing characters removed. For example, #!/usr/bin/python3 s = "\nab c\r\n" # str.rstrip([chars]) n = s....
Tomoki

How to Iterate Over a String by Character in Rust

In Rust, there are 3 ways to iterate over a string by character. Using chars Method The chars() method returns an iterator over the chars of a string slice. See the following example: fn main() { let s = "abc"; for c in s.chars() { pr...
pooriabt

How to Update a Value in a Mutable HashMap in Rust

In Rust, there are 3 ways to update a value in a mutable HashMap. Using get_mut Method The get_mut() method returns a mutable reference to the value corresponding to the key. See the following example: use std::collections::HashMap; fn main()...
Unused

How to Delete or Remove keys in a Map in Go

In Golang, using the delete function is the easiest way to delete keys in a map. Using delete Function Syntax The syntax of the delete function is shown below. func delete(m map[Type]Type1, key Type) delete Usage The delete built-in funct...
Patcher56

How to Pretty-Print Json in Go

In Golang, there are 2 ways to pretty-print json. Using json.MarshalIndent Function MarshalIndent is like Marshal but applies Indent to format the output. Each JSON element in the output will begin on a new line beginning with prefix followed by one...
ada

How to Get a Slice of Keys from a Map in Go

In Golang, there are 3 ways to get a slice of keys from a map.Using For Range The easiest way to get keys of map in Golang: package main import "fmt" func main() { m := map[string]int{ "a": 1, "b": 1, "c": 1, } // minimize ...
Sambhav Khandelwal