Several Ways to Stop Goroutine in Go.
A signal channel
Typically, you pass the goroutine a (possibly separate) signal channel. That signal channel is used to push a value into when you want the goroutine to stop. The goroutine polls that channel reg...
Catching return values from goroutines in Go.
Channels
A channel is a communication mechanism that lets one goroutine send values to another goroutine.
Make a main.go file containing the following:
package main
import (
"fmt"
"time"
)
func ...
Defer, Panic, and Recover in Go.
panic
During a typical panic, normal execution stops, all deferred function calls in that goroutine are executed, and the program crashes with a log message.
package main
func main() {
panic("invalid")
}
...
Closures and Anonymous Functions in Go.
Anonymous Function
A function literal is written like a function declaration, but without a name following the func keyword. It is an expression, and its value is called an anonymous function.
package main
...
Working with Constants and iota in Go.
The Constant Generator iota
Here’s an example from the time package, which defines named constants of type Weekday for the days of the week, starting with zero for Sunday. Types of this kind are often called en...
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 Rust, using the thread::sleep_ms method is the easiest way to put the current thread to sleep.
Using sleep_ms Method
The thread::sleep_ms() method puts the current thread to sleep for at least the specified amount of time. For example,
use ...
In Python, using the timedelta object is the easiest way to calculate number of days between two given dates.
Using timedelta
If you have two date objects, you can just subtract them, which computes a timedelta object.
In this program, we will cal...
In Python, there are 3 ways to get current time in milliseconds.
Using time Method
The time.time() method returns the time in seconds since the epoch as a floating point number. For example,
#!/usr/bin/python3
# Import module
import time
ms =...
In Python, there are 2 ways to get the day of week given a date.
Using weekday Method
The weekday() method returns the day of the week as an integer, where Monday is 0 and Sunday is 6.
The following example should cover whatever you are trying to ...
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, using the NumCPU function is the easiest way to find out the number of CPU's on a local machine.
Using runtime.NumCPU Function
The runtime.NumCPU() method returns the number of logical CPUs usable by the current process.
The set of avail...
In Python, using the timedelta object is the easiest way to subtract a day from a date.
Using timedelta Object
A timedelta object represents a duration, the difference between two dates or times.
See the following example:
#!/usr/bin/python3
#...
In Python, there are 3 ways to get file creation and modification dates times.
Using os.path Function
The os.path.getmtime() function returns the time of last modification of path.
The os.path.getctime() function returns the system’s ctime which, o...
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...