In Golang, using the Unmarshal JSON function is the easiest way to Marshal / Unmarshal json with a custom type attribute.
JSON (JavaScript Object Notation) is a lightweight data-interchange format.
Using UnmarshalJSON Function
Example on custom Unm...
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...
In Rust, using the parse method is the easiest way to convert a string to int.
Using parse Method
The str::parse::<T>() method parses this string slice into another type. For example,
fn main() {
let s = "100".to_string();
println!("{}"...
In Golang, there are 2 ways to get the difference in hours between two dates.
Using time.Since Function
The time.Since() function returns the time elapsed since t. It is shorthand for time.Now().Sub(t). For example,
package main
import (
"fm...
In Golang, there are 2 ways to validate URL with standard package.
Using ParseRequestURI Function
url.ParseRequestURI parses a raw url into a URL structure. It assumes that url was received in an HTTP request, so the url is interpreted only as an ab...
In Python, using the triple-quoted strings is the easiest way to create multiline comments.
Using triple-quoted Strings
String literals can span multiple lines. One way is using triple-quotes: """...""" or '''...'''.
You can use triple-quoted strin...
In Golang, there are 2 ways to access command-line arguments passed to a program.
Using os.Args Variable
You can access the command-line arguments using the os.Args variable.
Note that the first value in this slice is the path to the program, and o...
In Python, there are 2 ways to prettyprint a JSON file.
Using json module
The json module already implements some basic pretty printing in the dump and dumps functions, with the indent parameter that specifies how many spaces to indent by:
#!/us...
In Golang, using the time.Duration function is the easiest way to subtract time from a time.Time
Using time.Duration Function
A Duration represents the elapsed time between two instants as an int64 nanosecond count. The representation limits the la...
In Golang, using the time.Unix() function is the easiest way to parse unix timestamp to time.Time
Using time.Unix Function
You can directly use time.Unix function of time which converts the unix time stamp to UTC.
See the following example:
pa...
In Golang, there are 2 ways to pretty-print json.
Using json.MarshalIndent Function
MarshalIndent is like Marshal but applies Indent to format the output. Each JSON element in the output will begin on a new line beginning with prefix followed by one...
In Rust, there are 3 ways to convert string to json.
Using Serde JSON
Serde is a framework for serializing and deserializing Rust data structures efficiently and generically.
Adding dependencies
In Cargo.toml file we’ll add this information.
[de...
In Golang, there are two ways to get the start and end date of the current month.Using time.AddDate Function
AddDate returns the time corresponding to adding the given number of years, months, and days to t. For example,
package main
import (
"fm...
Fetch Internet Resources Using The urllib Package.Fetching URLs
The simplest way to use urllib.request is as follows:
#!/usr/bin/env python3
# Import datetime module
import urllib.request
with urllib.request.urlopen('https://google.com') as respon...
Converting Json to string in Python.Decoding JSON
To convert string to json in Python, use the json.loads() function. The json.loads() is a built-in Python function that accepts a valid json string and returns a dictionary to access all elements. The...