All Go Rust Python PHP JavaScript
Chrome Dev Summit to secure your spot in workshops, office hours and learning lounges!

How to Use Deferred Function Calls in Golang

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...
ada

How to Convert String to Rune in Golang

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...
Unused

How to Use Strings in Golang

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...
ada

How to Delete a Character from a String in Python

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...
Unused

How to Encrypt and Decrypt a String Using AES CBC in Go

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 ...
Tomoki

How to Encrypt and Decrypt a String Using AES GCM 256 in Go

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...
Tomoki

How to Generate a SHA256 HMAC Hash from a String in Go

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...
ada

How to Get a SHA256 Hash from a String in Go

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...
ada

How to Get a MD5 Hash from a String in Go

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...
aweis

How to check if a string contains a specific word in PHP

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...
ada

How to Check if a String Contains Whitespace in Rust

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() { ...
Unused

How to Do Case Insensitive String Comparison in Python

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...
pooriabt

How to Count Characters in a String in Go

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 ( ...
aweis

How to Remove the First Character of a String in Go

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" ...
Patcher56

How to Clear a bytes.Buffer in Go

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...
Unused