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 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 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 Rust, using the thread::sleep_ms method is the easiest way to put the current thread to sleep.
Using sleep_ms Method
The thread::sleep_ms() method puts the current thread to sleep for at least the specified amount of time. For example,
use ...
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, 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 buffer.Reset function is the easiest way to clear a bytes.Buffer
Using buffer.Reset Function
The buffer.Reset() function resets the buffer to be empty, but it retains the underlying storage for use by future writes. Reset is the...
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 Rust, using the read_dir function is the easiest way to list files of a directory.
Using read_dir Function
The fs::read_dir() function returns an iterator over the entries within a directory.
See the following example:
use std::fs;
fn main...
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 Golang, there are 2 ways to get file length.
Using file.Stat Function
Open opens the named file for reading. If successful, methods on the returned file can be used for reading; the associated file descriptor has mode O_RDONLY.
See the followin...
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 Python, there are 2 ways to prettyprint a JSON file.
Using json module
The json module already implements some basic pretty printing in the dump and dumps functions, with the indent parameter that specifies how many spaces to indent by:
#!/us...
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, there are 2 ways to set timeout for http.Get requests
Using http.Client.Timeout Field
Timeout specifies a time limit for requests made by this Client. The timeout includes connection time, any redirects, and reading the response body.
S...