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 ...
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.
...
Formatting float to currency string in Go.
Using localized formatting
Use golang.org/x/text/message to print using localized formatting for any language in the Unicode CLDR.
Make a main.go file containing the following:
package main
import (
"...
Introduction to Strings in Go.What is a String?
A string is an immutable sequence of bytes. Strings may contain arbitrary data, including bytes with value 0, but usually they contain human-readable text.
Make a main.go file containing the following...
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 duplicate slices.
Using append Function
You could write one simple statement to make a shallow copy of a slice.
The following example should cover whatever you are trying to do:
package main
import "fmt"
type T...
In Python, there are 2 ways to make a list of alphabet characters.
Using string Module
The string.ascii_lowercase is a pre-initialized string used as string constant. In Python, string ascii_lowercase will give the lowercase letters ‘abcdefghijklmno...
In Python, using the uniform function is the easiest way to get a random number between a float range.
Using uniform Method
The random.uniform(a, b) method returns a random floating point number N such that a
In Golang, using the crypto/aes package is the easiest way to encrypt a string using AES CBC.
Ehrsam, Meyer, Smith and Tuchman invented the cipher block chaining (CBC) mode of operation in 1976. In CBC mode, each block of plaintext is XORed with the ...
In Golang, using the crypto/aes package is the easiest way to encrypt a string using AES GCM 256.
In cryptography, Galois/Counter Mode (GCM) is a mode of operation for symmetric-key cryptographic block ciphers which is widely adopted for its performa...
In Golang, there are 3 ways to remove invalid UTF-8 characters from a string.
Using ToValidUTF8 Function
The strings.ToValidUTF8() function returns a copy of the string s with each run of invalid UTF-8 byte sequences replaced by the replacement str...
In Golang, there are 2 ways to check if a file is a valid image.
Using DetectContentType Function
The http.DetectContentType() function implements the algorithm described at https://mimesniff.spec.whatwg.org/ to determine the Content-Type of the gi...
In Python, there are 2 ways to get an absolute file path.
Using abspath Function
The os.path.abspath() method returns a normalized absolutized version of the pathname path. For example,
#!/usr/bin/python3
# Import module
import os
abs = os.pat...
In Golang, using the cases package is the easiest way to make first letter of words uppercase in a string.
Using cases Package
install
Use the following command to download the repository to the local file system.
install
go mod tidy
...