Skip to content

Instantly share code, notes, and snippets.

@guoxingx
guoxingx / Exercise-Errors.go
Created May 18, 2018 08:20
A Tour of Go - Exercise: Errors
package main
import (
"fmt"
)
type ErrNegativeSqrt float64
func (e ErrNegativeSqrt) Error() string {
return fmt.Sprintf("cannot Sqrt negative number: %v", float64(e))
@guoxingx
guoxingx / Exercise-Fibonacci-closure.go
Created May 18, 2018 07:55
A Tour of Go - Exercise: Fibonacci closure
package main
import "fmt"
// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
a, b, n := 0, 1, 0
return func() int {
a, b, n = b, a + b, n + 1
@guoxingx
guoxingx / Exercise-Stringers.go
Last active May 18, 2018 07:50
A Tour of Go - Exercise: Stringers
package main
import "fmt"
type IPAddr [4]byte
// TODO: Add a "String() string" method to IPAddr.
func (ip IPAddr) String() string {
return fmt.Sprintf("%v.%v.%v.%v", ip[0], ip[1], ip[2], ip[3])
}