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
...
Golang bytes.Buffer Examples
Examples of Golang bytes.Buffer
The bytes package provides the Buffer type for efficient manipulation of byte slices. A Buffer starts out empty but grows as data of types like string, byte, and []byte are written to it.
...
In PHP, using the $_SERVER variable is the easiest way to get the full URL.
Using $_SERVER Variable
$_SERVER is an array containing information such as headers, paths, and script locations.
The following example should cover whatever you are trying...
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...
In Python, using the os.environ variable is the easiest way to set environment variables
Using os.environ Variable
os.environ behaves like a python dictionary, so all the common dictionary operations can be performed. In addition to the get and set ...
In Rust, using the read_dir function is the easiest way to list files of a directory.
Using read_dir Function
The fs::read_dir() function returns an iterator over the entries within a directory.
See the following example:
use std::fs;
fn main...
In Golang, using the unsafe.Sizeof function is the easiest way to get memory size of the variable
Using Sizeof Function
You can use the unsafe.Sizeof function for this. It returns the size in bytes.
See the following example:
package main
impo...
In Python, there are 3 ways to check if a variable exists.
Using locals Function
The locals() function returns the dictionary of the current local symbol table.
#!/usr/bin/python3
vName = 1
if 'vName' in locals():
print("exists")
exists
...
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 Golang, there are 3 ways to read a whole file into a string variable.
Using ioutil.ReadFile Function
ReadFile reads the file named by filename and returns the contents. A successful call returns err == nil, not err == EOF. Because ReadFile reads...
In Python, there are 3 ways to remove an element from a list by index.
Using del Keyword
There is a way to remove an item from a list given its index instead of its value. For example,
#!/usr/bin/python3
# -*- coding: utf8 -*-
l = [1, 2, 3, 4]...
In Golang, there are 2 ways to install Go in Linux.Download Packages
Download packages for Windows 64-bit, macOS, Linux, and more.
Go install
Linux
Remove any previous Go installation by deleting the /usr/local/go folder (if it exists), then extr...
In Rust, variables by default are immutable.Variables Immutable
You can make them mutable by adding mut in front of the variable name. For example,
fn main() {
let x = 2;
println!("value x: {}", x);
x = 4;
println!("value x: {}", x);
}
...
In Rust, there are two ways to install rust in Linux.Running the following in your terminal
If you’re running macOS, Linux, or another Unix-like OS. To download Rustup and install Rust, run the following in your terminal, then follow the on-screen i...
Python String Formatting Best Practices.Fancier Output Formatting
To use formatted string literals, begin a string with f or F before the opening quotation mark or triple quotation mark. Inside this string, you can write a Python expression between {...