tutorial

package module
v0.0.0-...-500684f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 1, 2017 License: MIT Imports: 12 Imported by: 0

README

Go Tour: Exercise Solutions

Build Status Go Report Card codecov Godoc Reference

This repositoy contains my solutions to the Go Tour exercises including tests. Test coverage is 100% except for exercise 2 and exercise 9 which both draw pictures into the browser window.

Installation

go get github.com/afkegel/go-tutorial

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Crawl

func Crawl(url string, depth int, f *F, wg *sync.WaitGroup, fetcher Fetcher)

Crawl recursively crawls url to a maximum of depth. The caller has to provide a lockable map *F, that keeps track of scanned urls, a wait group *sync.Waitgroup to count goroutines, and an implementation of the Fetcher interface. Crawl fetches urls concurrently and skips previously fetched urls in case of duplicates. The caller is responsible for waiting until the goroutines return.

func Fibonacci

func Fibonacci() func() int

Fibonacci returns a closure that computes the next Fibonacci number with every subsequent call.

func Pic

func Pic(dx, dy int) [][]uint8

Pic returns png encoded data with an IMAGE tag that get parsed by the Go playground. https://play.golang.org/p/blLJbY6bFq It remains untested here.

func Same

func Same(t1, t2 *tree.Tree) bool

Same compares two trees for equality in the manner of reflect.DeepEqual. It converts trees to flat strings for comparison, which is not elegant but functional.

func Walk

func Walk(t *tree.Tree, ch chan int)

Walk traverses the tree from left to right and writes its elements to ch. It does not close so the channel is not rangable.

func WordCount

func WordCount(s string) map[string]int

WordCount counts the words in a string.

Types

type F

type F struct {
	// contains filtered or unexported fields
}

F is a lockable map that has to be provided to Crawl to cache fetched urls.

type Fetcher

type Fetcher interface {
	// Fetch returns the body of URL and a slice of URLs found on that page.
	Fetch(url string) (body string, urls []string, err error)
}

Fetcher abstracts the fetch method provided to Crawl.

type IPAddr

type IPAddr [4]byte

IPAddr is an exported IP address type

func (IPAddr) String

func (p IPAddr) String() string

A String() method for IPAddr

type Image

type Image struct {
}

Image implements the Image interface. Playground https://play.golang.org/p/wumbYwlBFl. Remains untested here.

func (Image) At

func (i Image) At(x, y int) color.Color

At returns the same color for all pixels (x, y).

func (Image) Bounds

func (i Image) Bounds() image.Rectangle

Bounds returnes a rectangle.

func (Image) ColorModel

func (i Image) ColorModel() color.Model

ColorModel returns an RBGA color model.

type Iterator

type Iterator int

Iterator sets the number of iterations for the Newton square root calculation.

type MyReader

type MyReader struct {
	// contains filtered or unexported fields
}

MyReader provides a reader to an embedded string.

func (*MyReader) Read

func (m *MyReader) Read(b []byte) (n int, err error)

Read method to MyReader

type Newton

type Newton interface {
	// contains filtered or unexported methods
}

Newton is an exported interface that has a sqrter

type Optimiser

type Optimiser float64

Optimiser sets the optimisation criteria for which the iterative Newton square root calculation stops.

type Rot13Reader

type Rot13Reader struct {
	// contains filtered or unexported fields
}

Rot13Reader provides methods to decode a rot13 encoded string to io.Reader

func (*Rot13Reader) Read

func (r13 *Rot13Reader) Read(p []byte) (i int, err error)

Decode method for a Rot13Reader returning the decoded string.

func (*Rot13Reader) String

func (r13 *Rot13Reader) String() string

String method for a Rot13Reader returning the undecoded string.

type SqrtFuncDecorated

type SqrtFuncDecorated func(float64) (float64, int, error)

SqrtFuncDecorated is the exported type for an error decorated square root function defined as func(float64)(float64, int).

func NewSqrtFuncDecorator

func NewSqrtFuncDecorator(f func(float64) (float64, int)) SqrtFuncDecorated

NewSqrtFuncDecorator is a constructor for a decorated square root function func(float64) (floa64, int).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL