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...
In Python, there are 2 ways to extract all the numbers contained in a string.
Using List Comprehension
List comprehensions provide a concise way to create lists.
If you only want to extract only positive integers, try the following:
#!/usr/bin/...
In Golang, there are 2 ways to copy a file.
Using io.Copy Function
You can copy a file using the io.Copy() function. For example,
package main
import (
"io"
"os"
)
// Copy the src file to dst.
func Copy(src, dst string) error {
in, err...
In Python, using the uuid module is the easiest way to create a GUID/UUID.
Using uuid Module
uuid module provides immutable UUID objects (the UUID class) and the functions uuid1(), uuid3(), uuid4(), uuid5() for generating version 1, 3, 4, and 5 UUID...
In Python, using the triple-quoted strings is the easiest way to create multiline comments.
Using triple-quoted Strings
String literals can span multiple lines. One way is using triple-quotes: """...""" or '''...'''.
You can use triple-quoted strin...
In Golang, using the os.O_APPEND flag is the easiest way to append text to a file
Using os.OpenFile Function
OpenFile is the generalized open call; most users will use Open or Create instead. It opens the named file with specified flag (O_RDONLY etc...
In Rust, there are 2 ways to index a string.
Using as_bytes Function
If the enum is C-like, then you can create a static array of each of the variants and return an iterator of references to them:
fn main() {
let s = "abc";
let b: u8 = s.as...
In Rust, there are 2 ways to iterate through the values of an enum.
Using a static array
If the enum is C-like, then you can create a static array of each of the variants and return an iterator of references to them:
use self::Direction::*;
use...
In Golang, using the os.MkdirAll function is the easiest way to create nested directories
Using os.MkdirAll Function
The os.MkdirAll() function creates a directory named path, along with any necessary parents, and returns nil, or else returns an er...
In Golang, using the io.copy function is the easiest way to download a large file
Using io.Copy Function
The io.Copy() function copies from src to dst until either EOF is reached on src or an error occurs. It returns the number of bytes copied and ...
In Rust, there are 2 ways to convert Vec to a string.
Using collect Function
The collect() function transforms an iterator into a collection. collect() can take anything iterable, and turn it into a relevant collection. This is one of the more powe...
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...
In Golang, there are 4 ways to generate a uuid.
Using uuid Package
install
Use the following command to download the repository to the local file system.
install
go mod tidy
go get github.com/google/uuid
go install github.com...
In Python, there are 3 ways to remove an element from a list by index.
Using del Keyword
There is a way to remove an item from a list given its index instead of its value. For example,
#!/usr/bin/python3
# -*- coding: utf8 -*-
l = [1, 2, 3, 4]...
In Python, there are 3 ways to merge two dictionaries into a new dictionary.
Using Additional Unpacking Generalizations
Starting with Python 3.5, we can merge in with literal notation as well. See the following example:
#!/usr/bin/python3
# -*-...