Skip to content

Instantly share code, notes, and snippets.

@brianjester
brianjester / exercise-equivalent-binary-trees.go
Last active January 15, 2018 02:27
Working with trees in golang
package main
import "fmt"
import "golang.org/x/tour/tree"
// Walk walks the tree t sending all values
// from the tree to the channel ch.
func Walk(t *tree.Tree, ch chan int) {
fmt.Println(t)
@brianjester
brianjester / exercise-rot-reader.go
Created October 9, 2017 00:10
A Tour of Go - Methods and Interfaces - Exercise: rot13Reader (23/26)
package main
import (
"io"
"os"
"strings"
)
type rot13Reader struct {
r io.Reader
@brianjester
brianjester / exercise-errors.go
Last active October 8, 2017 23:06
A Tour of Go - Methods and Interfaces - Exercise: Errors (20/26)
package main
import (
"fmt"
)
type ErrNegativeSqrt float64
func (e ErrNegativeSqrt) Error() string {
return fmt.Sprintf("cannot Sqrt negative number: %v",float64(e))
@brianjester
brianjester / exercise-loops-and-functions.go
Last active October 8, 2017 23:06
A Tour of Go - Basics - Flow Control Statements... - Exercise Loops and Functions (8/14) - Newton's Method of square root
package main
import (
"fmt"
)
func Sqrt(x float64) float64 {
result := 1.0
for count := 0 ;count < 10;count++ {
result = result - ((result*result - x)/(2*result))
@brianjester
brianjester / exercise-stringer.go
Created October 8, 2017 22:25
A Tour of Go - Methods and Interfaces - Exercise: Stringers (18/26)
package main
import "fmt"
type IPAddr [4]byte
func (ip IPAddr) String() string {
return fmt.Sprintf("%v.%v.%v.%v", ip[0],ip[1],ip[2],ip[3])
}
@brianjester
brianjester / exercise-slices.go
Created August 12, 2017 04:10
A Tour of Go - Exercise: Slices
package main
import "golang.org/x/tour/pic"
func Pic(dx, dy int) [][]uint8 {
mypic := make([][]uint8, dy)
for i := 0; i < dy; i++ {
mypic[i] = make([]uint8, dx)
for j := 0; j < dx; j++ {