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")
}
...
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...
An example program using numbers.
Integers And Floating Point Numbers
Go has several different types to represent numbers. Generally we split numbers into two different kinds: integers and floating-point numbers.
Make a main.go file containing the ...
In Golang, there are 2 ways to change the current directory.
Using Dir Property
Usually if you need a command to run from a specific directory, you can specify that as the Dir property on the Command, for example:
package main
import (
"os/...
In Python, there are 3 ways to get the system hostname.
Using gethostname Method
The socket.gethostname() method returns a string containing the hostname of the machine where the Python interpreter is currently executing. For example,
#!/usr/bin...
In Rust, there are 2 ways to convert float to integer.
Using as Keyword
Executing an as expression casts the value on the left-hand side to the type on the right-hand side.
An example of an as expression:
fn main() {
let a = 3.6415_f64;
l...
In Golang, there are 2 ways to pipe several commands.
Using piped commands
For example, this function retrieves the CPU model name using piped commands:
package main
import (
"fmt"
"os/exec"
)
func main() {
cmd := "cat /proc/cpuinfo | eg...
In Golang, using the exec.Command() function is the easiest way to execute a shell command
Using exec.Command Function
Command returns the Cmd struct to execute the named program with the given arguments.
You can execute a shell command using the e...
In Golang, there are 4 ways to generate a uuid.
Using uuid Package
install
Use the following command to download the repository to the local file system.
install
go mod tidy
go get github.com/google/uuid
go install github.com...
In Golang, there are 2 ways to list all standard packages.
Using packages
go install
Use the following command to download the repository to the local file system.
install
go get golang.org/x/tools/go/packages
go install golang.or...
In Rust, there are 2 ways to access command line parameters.
Using std::env::args Function
This function actually returns the arguments that this program was started with. See the following example:
use std::env;
fn main() {
for arg in env::...
In Golang, there are 3 ways to get the directory of the currently running file.Using os.Executable Function
Executable returns the path name for the executable that started the current process.
The easiest way to get the directory of the currently ...
In Python, there are 3 ways to execute a program or call a system command.
Using subprocess Module
The subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. See the following e...
In Python, there are 2 ways to make a time delay.
Using time.sleep Function
This function actually suspends execution of the calling thread for the given number of seconds. The argument may be a floating point number to indicate a more precise sleep...
In Python, there are 3 ways to check if a file exists.
Using os.path.exists Method
Return True if path refers to an existing path or an open file descriptor. Returns False for broken symbolic links. On some platforms, this function may return False ...