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 ...
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")
}
...
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...
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...
Closures and Anonymous Functions in Go.
Anonymous Function
A function literal is written like a function declaration, but without a name following the func keyword. It is an expression, and its value is called an anonymous function.
package main
...
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...
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...
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 (
"...
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 ...
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...
Understanding Rune in Go.
UTF-8 Decoder
The unicode/utf8 package provides one that we can use like this:
package main
import (
"fmt"
"unicode/utf8"
)
func main() {
s := "Hello, 世界"
for i := 0; i < len(s); {
r, size := utf8.DecodeR...
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 ...