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 Get correct file base name in Golang

Golang path and filepath Examples. Using filepath.Base The number is the index of the last slash in the string. If you want to get the file's base name, use filepath.Base: package main import ( "fmt" "path/filepath" ) func main() { path ...
Patcher56

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 Delete an Element from an Array in PHP

In PHP, there are 3 ways to delete an element from an array. Using unset Function The unset(mixed $var, mixed ...$vars): void function destroys the specified variables. For example, $arr = ["a", "b", "d"]; unset($arr[1]); print_r($arr); Arr...
Sambhav Khandelwal

How to Delete the Contents of a Folder in Python

In Python, there are 2 ways to delete the contents of a folder. Using rmtree Method The shutil.rmtree() method deletes an entire directory tree; path must point to a directory. For example, #!/usr/bin/python3 # Import module import os, shutil ...
Tomoki

How to Remove Invalid UTF-8 Characters from a String in Go

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

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 Remove all Empty Strings from a List of Strings in Python

In Python, there are 3 ways to remove all empty strings from a list of strings. Using filter Function The filter(function, iterable) function constructs an iterator from those elements of iterable for which function returns true. The following exam...
Sambhav Khandelwal

How to Remove an Element from a Vector in Rust

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

How to Remove all Whitespace in a String in Python

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

How to Delete a List Element by Value in Python

In Python, there are 3 ways to delete a list element by value. Using list.remove Function The list.remove(x) function removes the first item from the list whose value is equal to x. It raises a ValueError if there is no such item. See the following...
Unused

How to Move a File in Python

In Python, there are 3 ways to move a file. Using os Module The os.rename() method renames the file or directory src to dst. If dst exists, the operation will fail with an OSError subclass in a number of cases: #!/usr/bin/python3 # Import modu...
aweis

How to Remove Duplicates in List in Python

In Python, there are 3 ways to remove duplicates in list. Using Built-in set Function The built-in set() function returns a new set object, optionally with elements taken from iterable. If you later need a real list again, you can similarly pass the...
pooriabt

How to Get the Filename Without the Extension from a Path in Python

In Python, there are 2 ways to get the filename without the extension from a path. Using pathlib Module You can use .stem from pathlib to get the filename without the extension from a path, it really simple. For example, #!/usr/bin/python3 # Im...
Sambhav Khandelwal