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...
Understanding Rune in Go.
UTF-8 Decoder
The unicode/utf8 package provides one that we can use like this:
package main
import (
"fmt"
"unicode/utf8"
)
func main() {
s := "Hello, 世界"
for i := 0; i < len(s); {
r, size := utf8.DecodeR...
Introduction to Strings in Go.What is a String?
A string is an immutable sequence of bytes. Strings may contain arbitrary data, including bytes with value 0, but usually they contain human-readable text.
Make a main.go file containing the following...
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 Golang, using the crypto/aes package is the easiest way to encrypt a string using AES GCM 256.
In cryptography, Galois/Counter Mode (GCM) is a mode of operation for symmetric-key cryptographic block ciphers which is widely adopted for its performa...
In Python, there are 2 ways to find out the number of CPUs.
Using multiprocessing Module
The multiprocessing.cpu_count() method returns the number of CPUs in the system. For example,
#!/usr/bin/python3
# Import module
import multiprocessing
c...
In Golang, there are 2 ways to count characters in a string.
Using RuneCountInString Function
Straight forward natively use the utf8.RuneCountInString()
The following example should cover whatever you are trying to do:
package main
import (
...
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, 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...
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 Golang, there are 2 ways to iterate over a slice in reverse.
Using For Loop
There is no convenient operator for this to add to the range one in place. You'll have to do a normal for loop counting down.
See the following example:
package main...
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 Golang, using the time.Duration function is the easiest way to subtract time from a time.Time
Using time.Duration Function
A Duration represents the elapsed time between two instants as an int64 nanosecond count. The representation limits the la...
In Python, there are 3 ways to generate random integers between 0 and 9.
Using randrange Method
The random.randrange(start, stop[, step]) method returns a randomly selected element from range(start, stop, step). For example,
#!/usr/bin/python3
...