Skip to content

Instantly share code, notes, and snippets.

@bilsalak
bilsalak / exercise-reader.go
Created December 3, 2018 04:34
A Tour of Go - Exercise: Readers
// Implement a Reader type that emits an infinite stream of the ASCII character 'A'.
package main
import (
"golang.org/x/tour/reader"
)
type MyReader struct{}
// TODO: Add a Read([]byte) (int, error) method to MyReader.
@bilsalak
bilsalak / exercise-errors.go
Created December 2, 2018 23:04
A Tour of Go - Exercise: Errors
// Copy your Sqrt function from the earlier exercise and modify it to return an error value.
//
// Sqrt should return a non-nil error value when given a negative number, as it doesn't support complex numbers.
//
// Create a new type
//
// type ErrNegativeSqrt float64
//
// and make it an error by giving it a
//
@bilsalak
bilsalak / exercise-stringer.go
Created December 2, 2018 22:15
A Tour of Go - Exercise: Stringers
// Make the IPAddr type implement fmt.Stringer to print the address as a dotted quad.
//
// For instance, IPAddr{1, 2, 3, 4} should print as "1.2.3.4".
package main
import (
"fmt"
)
type IPAddr [4]byte
@bilsalak
bilsalak / exercise-fibonacci-closure.go
Created December 2, 2018 04:56
A Tour of Go - Exercise: Fibonacci closure
// Let's have some fun with functions.
//
// Implement a fibonacci function that returns a function (a closure) that returns
// successive fibonacci numbers (0, 1, 1, 2, 3, 5, ...).
package main
import "fmt"
// fibonacci is a function that returns
// a function that returns an int.
@bilsalak
bilsalak / exercise-maps.go
Created December 2, 2018 01:09
A Tour of Go - Exercise: Maps
// Implement WordCount. It should return a map of the counts of each “word” in the string s.
// The wc.Test function runs a test suite against the provided function and prints success or failure.
//
// You might find strings.Fields helpful.
package main
import (
"strings"
"golang.org/x/tour/wc"
@bilsalak
bilsalak / exercise-slices.go
Created December 2, 2018 00:59
A Tour of Go - Exercise: Slices
// Implement Pic. It should return a slice of length dy, each element of which is
// a slice of dx 8-bit unsigned integers. When you run the program, it will display your picture,
// interpreting the integers as grayscale (well, bluescale) values.
//
// The choice of image is up to you. Interesting functions include (x+y)/2, x*y, and x^y.
//
// (You need to use a loop to allocate each []uint8 inside the [][]uint8.)
//
// (Use uint8(intValue) to convert between types.)
package main
@bilsalak
bilsalak / exercise-loops-and-functions-part-3.go
Created December 2, 2018 00:40
A Tour of Go - Exercise: Loops and Functions - Part 3
// See https://gist.github.com/bilsalak/a53cddbee61647eea9de552193cfcedf for part 2
//
// Try other initial guesses for z, like x, or x/2. How close are your function's
// results to the math.Sqrt in the standard library?
package main
import (
"fmt"
"math"
)
@bilsalak
bilsalak / exercise-loops-and-functions-part-2.go
Created December 2, 2018 00:29
A Tour of Go - Exercise: Loops and Functions - Part 2
// See https://gist.github.com/bilsalak/a6016b2e564418402b1431491040f372 for part 1
//
// Next, change the loop condition to stop once the value has stopped changing
// (or only changes by a very small amount). See if that's more or fewer than 10 iterations.
package main
import (
"fmt"
"math"
)
@bilsalak
bilsalak / exercise-loops-and-functions-part-1.go
Last active December 2, 2018 00:23
A Tour of Go - Exercise: Loops and Functions - Part 1
// As a way to play with functions and loops, let's implement a square root function: given a number x,
// we want to find the number z for which z² is most nearly x.
//
// Computers typically compute the square root of x using a loop. Starting with some guess z,
// we can adjust z based on how close z² is to x, producing a better guess:
//
// z -= (z*z - x) / (2*z)
// Repeating this adjustment makes the guess better and better until we reach an answer that is as close
// to the actual square root as can be.
//