Understanding defer in Go.
Deferred Function Calls
A defer statement is an ordinary function or method call prefixed by the keyword defer. The function and argument expressions are evaluated when the statement is executed, but the actual call is def...
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 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 Golang, using the crypto/hmac library is the easiest way to generate a SHA256 HMAC Hash from a string.
In cryptography, an HMAC (hash-based message authentication code) is a specific type of message authentication code (MAC) involving a cryptograp...
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 PHP, there are 2 ways to check if a string contains a specific word.
Using strpos Function
The strpos(string $haystack, string $needle, int $offset = 0): int|false function finds the numeric position of the first occurrence of needle in the hayst...
In Rust, there are 2 ways to check if a string contains whitespace.
Using char::is_whitespace
char::is_whitespace returns true if the character has the Unicode White_Space property.
You can pass char::is_whitespace to .contains():
fn main() {
...
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 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 Golang, using the DecodeRuneInString function is the easiest way to remove the first character of a string.
Using DecodeRuneInString Function
The following example should cover whatever you are trying to do:
package main
import (
"fmt"
...
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...