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...
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.
...
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 Python, there are 2 ways to determine if an object is iterable.
Using Duck Typing
Duck typing is a concept related to dynamic typing, where the type or the class of an object is less important than the methods it defines. When you use duck typing...
In Golang, using the len function is the easiest way to count the items in the map structure.
Using len Function
The built-in function len take arguments of various types and return a result of type int. The implementation guarantees that the result...
In Python, there are 2 ways to check if an object is of a given type.
Using isinstance Method
The isinstance(object, classinfo) method returns True if the object argument is an instance of the classinfo argument, or of a (direct, indirect, or virtua...
In Golang, there are 2 ways to get the maximum value for an int type.
Using constant values
Since integer types use two's complement arithmetic, you can infer the min/max constant values for int and uint. For example,
package main
import (
"...
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/...
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 2 ways to remove duplicates strings from slice.Using a Hash Map
We have defined a function where we are passing the slice values and using the map function we are checking the duplicates and removing them. The ones that are curr...
In Python, there are 2 ways to convert string to int.
Using int Built-in Function
Return an integer object constructed from a number or string x, or return 0 if no arguments are given.
#!/usr/bin/python3
# -*- coding: utf8 -*-
s = "100"
# clas...
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...
Rust String Formatting and Printing Best Practices.Using println!
Prints to the standard output, with a newline. Use the format! syntax to write data to the standard output. See std::fmt for more information.
fn main() {
let name = "Bob";
pri...
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...