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.
...
Calculate degrees celcius from farenheit in Go.
Following program shows you how to convert fahrenheit to celsius.
The function FToC below encapsu- lates the temperature conversion logic so that it is defined only once but may be used from multiple p...
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 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 Python, there are 2 ways to convert all strings in a list to int.
Using map Method
You can convert all strings in a list to int using the map method. For example,
#!/usr/bin/python3
a = ["1", "2", "4", "5"]
b = list(map(int, a))
print(b)
...
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 Rust, there are 2 ways to convert a str to a &[u8].
Using as_bytes Function
The as_bytes() method converts a string slice to a byte slice. To convert the byte slice back into a string slice, use the from_utf8 function.
See the following example...
In Golang, there are 2 ways to convert a bool to a string.
Using strconv.FormatBool Function
The strconv.FormatBool() function returns "true" or "false" according to the value of b. For example,
package main
import (
"fmt"
"strconv"
)
fun...
In Golang, using the float64 function is the easiest way to convert an integer to a float number.
Using float64 Function
The easiest way to convert an integer to a float number in Golang:
package main
import "fmt"
func main() {
i := 6
f ...
In Python, there are 3 ways to read from stdin.
Using fileinput Module
This module implements a helper class and functions to quickly write a loop over standard input or a list of files. For example,
#!/usr/bin/python3
# Import module
import f...
In Rust, there are 2 ways to convert Vec to a string.
Using collect Function
The collect() function transforms an iterator into a collection. collect() can take anything iterable, and turn it into a relevant collection. This is one of the more powe...
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 Rust, there are 3 ways to convert a string into a &'static str.
Using slicing Syntax
To go from a String to a slice &'a str you can use slicing syntax. See the following example:
// Rust 1.0+
fn main() {
let s: String = "abc".to_owned();
...
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 Python, there are 2 ways to get the current time.
Using datetime Module
The datetime module supplies classes for manipulating dates and times. See the following example:
#!/usr/bin/python3
# -*- coding: utf8 -*-
# Import module
import datet...