Understanding defer in Go.
Deferred Function Calls
A defer statement is an ordinary function or method call prefixed by the keyword defer. The function and argument expressions are evaluated when the statement is executed, but the actual call is def...
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 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 Golang, using the runtime.FuncForPC function is the easiest way to get the name of a function
Using runtime.FuncForPC Function
The runtime.FuncForPC() function returns a *Func describing the function that contains the given program counter addres...
In Python, there are 2 ways to get the filename without the extension from a path.
Using pathlib Module
You can use .stem from pathlib to get the filename without the extension from a path, it really simple. For example,
#!/usr/bin/python3
# Im...
In Python, there are 2 ways to extract extension from filename.
Using os.path.splitext Method
The os.path.splitext(path) method splits the pathname path into a pair (root, ext) such that root + ext == path, and the extension, ext, is empty or begins...
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()...
In Python, there are 4 ways to know if an object has an attribute.
Using hasattr Method
The arguments are an object and a string. The result is True if the string is the name of one of the object’s attributes, False if not. For example,
#!/usr/...
In Rust, there are 2 ways to check if key exists in HashMap.
Using HashMap::contains_key Method
The contains_key method return true if the map contains a value for the specified key.
use std::collections::HashMap;
fn main() {
let mut map = Hash...
In Rust, there are 3 ways to check if string contains substring.Using String contains Function
The easiest way to check if a Rust string contains a substring is to use String::contains method.
The contains method Returns true if the given pattern m...
In Golang, there are two ways to format the time.The layout string
Go doesn’t use yyyy-mm-dd layout to format a time. Instead, you format a special layout parameter.
2006-01-02 15:04:05.999999999 -0700 MST
package main
import (
"fmt"
"tim...
3 ways to initialize a Python Array .Initialize a list with values
If you want to initialize a list of any number of elements where all elements are filled with any values, you can use the * operator as follows.
#!/usr/bin/env python3
arr = ['H'] *...