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 Recover panic in Golang

Defer, Panic, and Recover in Go. panic During a typical panic, normal execution stops, all deferred function calls in that goroutine are executed, and the program crashes with a log message. package main func main() { panic("invalid") } ...
Unused

How to Use Deferred Function Calls in Golang

Understanding defer in Go. Deferred Function Calls A defer statement is an ordinary function or method call prefixed by the keyword defer. The function and argument expressions are evaluated when the statement is executed, but the actual call is def...
ada

How to Use Variadic Functions in Golang

Variadic Functions Tutorial with Examples in Go. Variadic Function A variadic function is one that can be called with varying numbers of arguments. The most familiar examples are fmt.Printf and its variants. package main import "fmt" func add(v...
Patcher56

How to Compare if two Maps are Equal in Golang

Testing the equivalence of maps in Go. reflect.DeepEqual Function It first checks if both maps are nil, then it checks if they have the same length before finally checking to see if they have the same set of (key, value) pairs. package main impo...
Patcher56

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 Convert the Fahrenheit to Celsius in Golang

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...
Sambhav Khandelwal

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 Check if a Slice Contains an Element in Go

In Golang, there are 2 ways to check if a slice contains an element. Using contains Function The following example should cover whatever you are trying to do: package main import "fmt" func Contains[E comparable](s []E, v E) bool { for _, ...
ada

How to Determine Whether a Given Integer is Between two Other Integers in Python

In Python, there are 2 ways to determine whether a given integer is between two other integers. Using Comparisons Unlike C, all comparison operations in Python have the same priority, which is lower than that of any arithmetic, shifting or bitwise o...
ada

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 Extract All the Numbers Contained in a String in Python

In Python, there are 2 ways to extract all the numbers contained in a string. Using List Comprehension List comprehensions provide a concise way to create lists. If you only want to extract only positive integers, try the following: #!/usr/bin/...
Sambhav Khandelwal

How to Generate a SHA256 HMAC Hash from a String in Go

In Golang, using the crypto/hmac library is the easiest way to generate a SHA256 HMAC Hash from a string. In cryptography, an HMAC (hash-based message authentication code) is a specific type of message authentication code (MAC) involving a cryptograp...
ada

How to Get a MD5 Hash from a String in Go

In Golang, using the Sum function is the easiest way to get a MD5 hash from a string. Using Sum Function The md5.Sum() function returns the MD5 checksum of the data. The following example should cover whatever you are trying to do: package mai...
aweis