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 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 Use the Constant Generator iota in Golang

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

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 Get correct file base name in Golang

Golang path and filepath Examples. Using filepath.Base The number is the index of the last slash in the string. If you want to get the file's base name, use filepath.Base: package main import ( "fmt" "path/filepath" ) func main() { path ...
Patcher56

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 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 sort a vector in Rust

In Rust, there are 2 ways to sort a vector. Using sort Method The sort(&mut self) method sorts the slice. This sort is stable (i.e., does not reorder equal elements) and O(n * log(n)) worst-case. For example, fn main() { let mut v = [3, -5, ...
Tomoki

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 Sort a Slice in Go

In Golang, using the sort.Slice function is the easiest way to sort a slice Using Slice Function You simply pass an anonymous function to the sort.Slice function: package main import ( "fmt" "sort" ) func main() { s := []string{"Houston...
Tomoki

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