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 Check if a Slice Contains an Element in Go

In Golang, there are 2 ways to check if a slice contains an element. Using contains Function The following example should cover whatever you are trying to do: package main import "fmt" func Contains[E comparable](s []E, v E) bool { for _, ...
ada

How to Check String is In Json Format in Go

In Golang, there are 2 ways to check string is in json format. Using Unmarshal Function The following example should cover whatever you are trying to do: package main import ( "encoding/json" "fmt" ) func isJson(s string) bool { var js ...
pooriabt

How to Check if IP Address is in Private Network Space in Go

In Golang, using the IsPrivate function is the easiest way to check if IP address is in private network space. Using IsPrivate Function The IsPrivate() function reports whether ip is a private address, according to RFC 1918 (IPv4 addresses) and RFC...
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 Get the Name of a Function in Go

In Golang, using the runtime.FuncForPC function is the easiest way to get the name of a function Using runtime.FuncForPC Function The runtime.FuncForPC() function returns a *Func describing the function that contains the given program counter addres...
Unused

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

How to Extract Extension from Filename in Python

In Python, there are 2 ways to extract extension from filename. Using os.path.splitext Method The os.path.splitext(path) method splits the pathname path into a pair (root, ext) such that root + ext == path, and the extension, ext, is empty or begins...
Unused

How to Update a Value in a Mutable HashMap in Rust

In Rust, there are 3 ways to update a value in a mutable HashMap. Using get_mut Method The get_mut() method returns a mutable reference to the value corresponding to the key. See the following example: use std::collections::HashMap; fn main()...
Unused

How to Know If an Object has an Attribute in Python

In Python, there are 4 ways to know if an object has an attribute. Using hasattr Method The arguments are an object and a string. The result is True if the string is the name of one of the object’s attributes, False if not. For example, #!/usr/...
ada

How to check if key exists in HashMap in Rust

In Rust, there are 2 ways to check if key exists in HashMap. Using HashMap::contains_key Method The contains_key method return true if the map contains a value for the specified key. use std::collections::HashMap; fn main() { let mut map = Hash...
Unused

How to check if string contains substring in Rust

In Rust, there are 3 ways to check if string contains substring.Using String contains Function The easiest way to check if a Rust string contains a substring is to use String::contains method. The contains method Returns true if the given pattern m...
Tomoki

How to format a date or time in Go

In Golang, there are two ways to format the time.The layout string Go doesn’t use yyyy-mm-dd layout to format a time. Instead, you format a special layout parameter. 2006-01-02 15:04:05.999999999 -0700 MST package main import ( "fmt" "tim...
aweis

How to fill a list with same values in Python

3 ways to initialize a Python Array .Initialize a list with values If you want to initialize a list of any number of elements where all elements are filled with any values, you can use the * operator as follows. #!/usr/bin/env python3 arr = ['H'] *...
aweis