Variadic Functions Tutorial with Examples in Go.
Variadic Function
A variadic function is one that can be called with varying numbers of arguments. The most familiar examples are fmt.Printf and its variants.
package main
import "fmt"
func add(v...
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...
Working with Constants and iota in Go.
The Constant Generator iota
Here’s an example from the time package, which defines named constants of type Weekday for the days of the week, starting with zero for Sunday. Types of this kind are often called en...
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 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 Python, there are 2 ways to append integer to beginning of list.
Using insert Method
The list.insert() method inserts an item at a given position. The first argument is the index of the element before which to insert. For example,
#!/usr/bin...
In Python, using the timedelta object is the easiest way to calculate number of days between two given dates.
Using timedelta
If you have two date objects, you can just subtract them, which computes a timedelta object.
In this program, we will cal...
In Python, there are 2 ways to capitalize the first letter of each word in a string.
Using title Method
The str.title() method returns a titlecased version of the string where words start with an uppercase character and the remaining characters are...
In Python, there are 2 ways to get the day of week given a date.
Using weekday Method
The weekday() method returns the day of the week as an integer, where Monday is 0 and Sunday is 6.
The following example should cover whatever you are trying to ...
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 Golang, using the DecodeRuneInString function is the easiest way to remove the first character of a string.
Using DecodeRuneInString Function
The following example should cover whatever you are trying to do:
package main
import (
"fmt"
...
In Rust, there are 2 ways to remove an element from a vector.
Using retain Function
The vec::retain() function retains only the elements specified by the predicate.
For example,
fn main() {
let mut v = vec![1, 3, 2, 3, 4];
v.retain(|&x| x ...
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
...
In Python, using the timedelta object is the easiest way to subtract a day from a date.
Using timedelta Object
A timedelta object represents a duration, the difference between two dates or times.
See the following example:
#!/usr/bin/python3
#...
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...