In Golang, there are 2 ways to duplicate slices.
Using append Function
You could write one simple statement to make a shallow copy of a slice.
The following example should cover whatever you are trying to do:
package main
import "fmt"
type T...
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 Sum256 function is the easiest way to get a SHA256 hash from a string.Go implements several hash functions in various crypto/* packages.
Using Sum256 Function
The sha256.Sum256() function returns the SHA256 checksum of the data...
In Golang, using the Sum function is the easiest way to get a MD5 hash from a string.
Using Sum Function
The md5.Sum() function returns the MD5 checksum of the data.
The following example should cover whatever you are trying to do:
package mai...
In Python, there are 2 do case insensitive string comparison.
Using casefold Method
The str.casefold() method returns a casefolded copy of the string. Casefolded strings may be used for caseless matching. For example,
#!/usr/bin/python3
a = "H...
In Golang, there are 3 ways to remove invalid UTF-8 characters from a string.
Using ToValidUTF8 Function
The strings.ToValidUTF8() function returns a copy of the string s with each run of invalid UTF-8 byte sequences replaced by the replacement str...
In Golang, there are 2 ways to pipe several commands.
Using piped commands
For example, this function retrieves the CPU model name using piped commands:
package main
import (
"fmt"
"os/exec"
)
func main() {
cmd := "cat /proc/cpuinfo | eg...
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, there are 3 ways to remove all whitespace in a string.
Using str.replace Function
The str.replace(x) function returns a copy of the string with all occurrences of substring old replaced by new.
See the following example:
#!/usr/bin/p...
In Python, there are 4 ways to copy a dictionary.
Using dict Method
If you want to copy the dict, you have to do so explicitly with:
#!/usr/bin/python3
a = {"a": 1, "b": 2}
# Copy
b = dict(a)
b['a'] = 3
print(a)
print(b)
{'a': 1, 'b': 2}
{'...
In Python, there are 3 ways to reverse a string.
Using slicing
Slice notation takes the form [start:stop:step].
#!/usr/bin/python3
s = "Hello World"
print(s[::-1])
# syntax
# a[start:stop] # items start through stop-1
# a[start:] # items...
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 Python, there are 3 ways to pad a numeric string with zeros to the left.
Using zfill Method
The str.zfill(width) method returns a copy of the string left filled with ASCII '0' digits to make a string of length width. For example,
#!/usr/bin/...
In Python, there are 4 ways to remove a trailing newline.
Using rstrip Method
The str.rstrip() method returns a copy of the string with trailing characters removed. For example,
#!/usr/bin/python3
s = "\nab c\r\n"
# str.rstrip([chars])
n = s....
In Golang, there are 3 ways to read a whole file into a string variable.
Using ioutil.ReadFile Function
ReadFile reads the file named by filename and returns the contents. A successful call returns err == nil, not err == EOF. Because ReadFile reads...