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...
Golang bytes.Buffer Examples
Examples of Golang bytes.Buffer
The bytes package provides the Buffer type for efficient manipulation of byte slices. A Buffer starts out empty but grows as data of types like string, byte, and []byte are written to it.
...
An example program using numbers.
Integers And Floating Point Numbers
Go has several different types to represent numbers. Generally we split numbers into two different kinds: integers and floating-point numbers.
Make a main.go file containing the ...
In Golang, using the crypto/aes package is the easiest way to encrypt a string using AES CBC.
Ehrsam, Meyer, Smith and Tuchman invented the cipher block chaining (CBC) mode of operation in 1976. In CBC mode, each block of plaintext is XORed with the ...
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 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 ...
In Golang, using the %p verbs is the easiest way to print the memory address of a slice.
Using fmt.Printf Function
Pointer:
%p base 16 notation, with leading 0x. The %b, %d, %o, %x and %X verbs also work with pointers,
formatting the value exactly ...
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 ...
In Python, using the str.join function is the easiest way to concatenate a list of strings into a single string
Using str.join Function
The str.join() method returns a string which is the concatenation of the strings in iterable. A TypeError will be...
In Python, there are 2 ways to get a function name as a string.
Using __name__ Attribute
Using __qualname__ is the preferred method as it applies uniformly. It works on built-in functions as well:
#!/usr/bin/python3
def my_func():
pass
class...
In Rust, there are 2 ways to reverse a string.
Using rev Function
The str::rev() function reverses an iterator’s direction.
See the following example:
fn main() {
let mut s = "Hello";
println!("{}", s.chars().rev().collect::<String>());...
In Rust, there are 2 ways to get the string length in characters.
Using chars Function
The str::chars() function returns an iterator over the chars of a string slice.
See the following example:
fn main() {
let mut s = "Hello";
println!("{}...
In Golang, there are 2 ways to get the difference in hours between two dates.
Using time.Since Function
The time.Since() function returns the time elapsed since t. It is shorthand for time.Now().Sub(t). For example,
package main
import (
"fm...
In Golang, using the a case-insensitive flag is the easiest way to do a case insensitive regular expression.
Using a case-insensitive flag
You can set a case-insensitive flag as the first item in the regex.
You can add a (?i) at the beginning of th...
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...