Skip to content

Instantly share code, notes, and snippets.

@pake007
Last active December 15, 2015 21:19
Show Gist options
  • Save pake007/5325314 to your computer and use it in GitHub Desktop.
Save pake007/5325314 to your computer and use it in GitHub Desktop.
A Tour of Go, my code of exercises
// Implement fibonacci by using go closure
package main
import "fmt"
func fibonacci() func(int) int {
var sum1, sum2 int = 0, 1
return func(x int) int {
if x == 0 {
return sum1
}
if x == 1 {
return sum2
}
sum1, sum2 = sum2, sum1 + sum2
return sum2
}
}
func main() {
f := fibonacci()
for i := 0; i < 10; i++ {
fmt.Println(f(i))
}
}
// Implement and define ServeHTTP methods on a String and Struct.
// Register them to handle specific paths in your web server.
package main
import (
"fmt"
"net/http"
)
type String string
type Struct struct {
Greeting string
Punct string
Who string
}
func (s String) ServeHTTP(
w http.ResponseWriter,
r *http.Request) {
fmt.Fprint(w, s)
}
func (s *Struct) ServeHTTP(
w http.ResponseWriter,
r *http.Request) {
fmt.Fprint(w, s.Greeting, s.Punct, s.Who)
}
func main() {
// your http.Handle calls here
http.Handle("/string", String("I'm a frayed knot."))
http.Handle("/struct", &Struct{"Hello", ":", "Gophers!"})
http.ListenAndServe("localhost:4000", nil)
}
// Define and implement the image interface
package main
import (
"code.google.com/p/go-tour/pic"
"image"
"image/color"
)
type Image struct {
width int
height int
color uint8
}
func (m *Image) ColorModel() color.Model {
return color.RGBAModel
}
func (m *Image) Bounds() image.Rectangle {
return image.Rect(0, 0, m.width, m.height)
}
func (m *Image) At(x, y int) color.Color {
return color.RGBA{m.color, m.color, 255, 255}
}
func main() {
m := &Image{50, 50, 100}
pic.ShowImage(m)
}
// As a simple way to play with functions and loops,
// implement the square root function using Newton's method.
package main
import (
"fmt"
)
func Sqrt(x float64) float64 {
z := 1.0
for i := 0; i < 10000000; i++ {
z = z - (z * z - x) / 2 * z
}
return z
}
func main() {
fmt.Println(Sqrt(2))
}
// Sqrt should return a non-nil error value when given a negative number,
// as it doesn't support complex numbers.
package main
import (
"fmt"
)
type ErrNegativeSqrt float64
func (e ErrNegativeSqrt) Error() string {
return fmt.Sprintf("cannot Sqrt negative number: %f", float64(e))
}
func Sqrt(f float64) (float64, error) {
var result float64
var err error
if f < 0 {
result = f
err = ErrNegativeSqrt(f)
} else {
z := 1.0
for i := 0; i < 10000000; i++ {
z = z - (z * z - f) / 2 * z
}
result = z
err = nil
}
return result, err
}
func main() {
fmt.Println(Sqrt(2))
fmt.Println(Sqrt(-2))
}
// Implement a rot13Reader that implements io.Reader and reads from an io.Reader,
// modifying the stream by applying the ROT13 substitution cipher to all alphabetical characters.
package main
import (
"io"
"os"
"strings"
)
type rot13Reader struct {
r io.Reader
}
func rot13(c byte) byte {
if c >= 'a' && c <= 'm' || c >= 'A' && c <= 'M' {
return c + 13
} else if c >= 'n' && c <= 'z' || c >= 'N' && c <= 'Z' {
return c - 13
}
return c
}
func (rot *rot13Reader) Read(p []byte) (n int, err error) {
n, err = rot.r.Read(p)
for i := 0; i < n; i++ {
p[i] = rot13(p[i])
}
return
}
func main() {
s := strings.NewReader(
"Lbh penpxrq gur pbqr!")
r := rot13Reader{s}
io.Copy(os.Stdout, &r)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment