All Go Rust Python PHP JavaScript
Chrome Dev Summit to secure your spot in workshops, office hours and learning lounges!

How to Stop a goroutine in Golang

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...
pooriabt

How to Fetch Return Value from a goroutine in Golang

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 ...
Tomoki

How to Use bytes.Buffer in Golang

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. ...
ada

How to Format a Currency with Commas in Golang

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 ( "...
Unused

How to Use Strings in Golang

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...
ada

How to Add Two Numbers in Go

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 ...
pooriabt

How to Make a Shallow Copy of a Slice in Go

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...
Unused

How to Make a List of Alphabet Characters in Python

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...
Patcher56

How to Get a Random Number Between a Float Range in Python

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
Unused

How to Encrypt and Decrypt a String Using AES CBC in Go

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 ...
Tomoki

How to Encrypt and Decrypt a String Using AES GCM 256 in Go

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...
Tomoki

How to Remove Invalid UTF-8 Characters from a String in Go

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...
pooriabt

How to Check if a File is a Valid Image in Go

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...
pooriabt

How to Get an Absolute File Path in Python

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...
aweis

How to Make First Letter of Words Uppercase in a String in Go

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 ...
pooriabt