Skip to content

Instantly share code, notes, and snippets.

@kaipakartik
kaipakartik / tree.go
Created December 25, 2013 07:00
Exercise: Equivalent Binary Trees
package main
import (
"code.google.com/p/go-tour/tree"
"fmt"
)
// Walk walks the tree t sending all values
// from the tree to the channel ch.
func Walk(t *tree.Tree, ch chan int) {
@kaipakartik
kaipakartik / rot13.go
Created December 25, 2013 02:21
Exercise: Rot13 Reader A common pattern is an io.Reader that wraps another io.Reader, modifying the stream in some way.
package main
import (
"io"
"os"
"strings"
)
type rot13Reader struct {
r io.Reader
@kaipakartik
kaipakartik / image.go
Created December 25, 2013 02:04
Exercise: Images Remember the picture generator you wrote earlier? Let's write another one, but this time it will return an implementation of image.Image instead of a slice of data.
package main
import (
"code.google.com/p/go-tour/pic"
"image/color"
"image"
)
type Image struct{
width, height int
@kaipakartik
kaipakartik / gist:8119470
Created December 25, 2013 01:42
Exercise: HTTP Handlers Implement the following types and define ServeHTTP methods on them. Register them to handle specific paths in your web server. http://tour.golang.org/#58
package main
import (
"fmt"
"net/http"
)
type String string
type Struct struct {
@kaipakartik
kaipakartik / errors.go
Created December 25, 2013 01:36
Exercise: Errors Copy your Sqrt function from the earlier exercises 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 http://tour.golang.org/#56
package main
import (
"fmt"
"math"
)
type ErrNegativeSqrt float64
func (e ErrNegativeSqrt) Error() string {
@kaipakartik
kaipakartik / complexpower.go
Created December 25, 2013 01:18
Advanced Exercise: Complex cube roots http://tour.golang.org/#48
package main
import (
"fmt"
"math/cmplx"
)
func Cbrt(x complex128) complex128 {
z := complex128(1)
for i := 0; i < 10; i++ {
@kaipakartik
kaipakartik / fibonacci.go
Created December 25, 2013 01:09
Solution to the fibonacci problem using function closures in go http://tour.golang.org/#44
package main
import "fmt"
// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
current := 0
a := 0
b := 1
@kaipakartik
kaipakartik / wordcount.go
Created December 25, 2013 01:01
Solution to the maps exercise on golang http://tour.golang.org/#41
package main
import (
"code.google.com/p/go-tour/wc"
"strings"
)
func WordCount(s string) map[string]int {
words := strings.Fields(s)
wordcount := make(map[string]int)
@kaipakartik
kaipakartik / slices.go
Created December 25, 2013 00:55
Solution to the slices exercise in Go
package main
import "code.google.com/p/go-tour/pic"
func Pic(dx, dy int) [][]uint8 {
y := make([][]uint8, dy)
for i := range y {
y[i] = make([]uint8, dx)
}
y = transform(y)
@kaipakartik
kaipakartik / loops.go
Last active January 1, 2016 08:39
My solution to the loops and functions exercise http://tour.golang.org/#24
package main
import (
"fmt"
"math"
)
func Sqrt(x float64) float64 {
next := float64(1)
prev := float64(0)