A Tour of Go: Exercises

Exercise: Loops and Functions

package main

import (
	"fmt"
	"math"
)

func Sqrt(x float64) float64 {
	z := 1.0
	for i := 1; i < 10; i++ {
		z -= (z*z - x) / (2*z)
	}
	return z
}

func main() {
	fmt.Println(Sqrt(2))
	fmt.Println(math.Sqrt(2))
}

Exercise: Slices

package main

import "golang.org/x/tour/pic"

func Pic(dx, dy int) [][]uint8 {
	pixels := make([][]uint8, dy)
    for y := 0; y < dy; y++ {
        pixels[y] = make([]uint8, dx)
        for x := 0; x < dx; x++ {
			pixels[y][x] = uint8(x^y)
        }
    }
    return pixels
}

func main() {
	pic.Show(Pic)
}

Exercise: Fibonacci closure

package main

import "fmt"

// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
	a, b := -1, 1
	
	return func() int {
		a, b = b, a + b
		
		return b
	}
}

func main() {
	f := fibonacci()
	for i := 0; i < 10; i++ {
		fmt.Println(f())
	}
}

Exercise: Stringers

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])
}

func main() {
	hosts := map[string]IPAddr{
		"loopback":  {127, 0, 0, 1},
		"googleDNS": {8, 8, 8, 8},
	}
	for name, ip := range hosts {
		fmt.Printf("%v: %v\n", name, ip)
	}
}

Exercise: Errors

package main

import (
	"fmt"
)

type ErrNegativeSqrt float64

func (e ErrNegativeSqrt) Error() string {
	return fmt.Sprintf("cannot Sqrt negative number: %v", float64(e))
}

func Sqrt(x float64) (float64, error) {
	switch {
		case x < 0:
			return 0, ErrNegativeSqrt(x)
		default:
			z := 1.0
			for i := 1; i < 10; i++ {
				z -= (z*z - x) / (2*z)
			}
			return z, nil
	}
}

func main() {
	fmt.Println(Sqrt(2))
	fmt.Println(Sqrt(-2))
}

Exercise: Readers

package main

import "golang.org/x/tour/reader"

type MyReader struct{}

func (MyReader) Read(b []byte) (n int, err error) {
	b[0] = 'A';
	return 1, nil;
}

func main() {
	reader.Validate(MyReader{})
}

Exercise: rot13Reader

package main

import (
	"io"
	"os"
	"strings"
)

type rot13Reader struct {
	r io.Reader
}

func (rot13Reader *rot13Reader) Read(bytes []byte) (int, error) {
	n, err := rot13Reader.r.Read(bytes)
    for i := 0; i <= n; i++ {
        bytes[i] = rot13(bytes[i])
    }
    return n, err
}

func rot13(b byte) byte {
	if 'A' <= b && b <= 'Z' {
		b = b + 13
		if (b > 'Z') {
			return b - 26
		}
	}
	
	if 'a' <= b && b <= 'z' {
		b = b + 13
		if (b > 'z') {
			return b - 26
		}
	}
	
	return b
}

func main() {
	s := strings.NewReader("Lbh penpxrq gur pbqr!")
	r := rot13Reader{s}
	io.Copy(os.Stdout, &r)
}

Exercise: Images

package main

import "image"
import "image/color"
import "golang.org/x/tour/pic"

type Image struct {
	width, height int
	color uint8
	opacity uint8
}

func (img *Image) Bounds() image.Rectangle {
	return image.Rect(0, 0, img.width, img.height)
}

func (img *Image) ColorModel() color.Model {
	return color.RGBAModel
}

func (img *Image) At(x, y int) color.Color {
	return color.RGBA{uint8(x^y), uint8(x^y), img.color, img.opacity}
}

func main() {
	m := Image{512, 512, 255, 255}
	pic.ShowImage(&m)
}

Exercise: Equivalent Binary Trees

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 Walk2(t *tree.Tree, ch chan int) {
	
}

func Walk(t *tree.Tree, ch chan int) {
	walkRecur(t, ch)
	close(ch)
}

func walkRecur(t *tree.Tree, ch chan int) {
	if t.Left != nil {
		walkRecur(t.Left, ch)
	}
	
	ch <- t.Value
	
	if t.Right != nil {
		walkRecur(t.Right, ch)
	}
}

// Same determines whether the trees
// t1 and t2 contain the same values.
func Same(t1, t2 *tree.Tree) bool {
	ch1, ch2 := make(chan int), make(chan int)
	go Walk(t1, ch1)
	go Walk(t2, ch2)
	for i := range ch1 {
		if i != <-ch2 {
			return false
		}
	}
	return true
}

func main() {
	ch := make(chan int)
	go Walk(tree.New(1), ch)
	for i := range ch {
		fmt.Println(i)
	}
	fmt.Println(Same(tree.New(1), tree.New(1)))
	fmt.Println(Same(tree.New(1), tree.New(2)))
}

Exercise: Web Crawler

package main

import (
	"fmt"
)

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)
}

// Crawl uses fetcher to recursively crawl
// pages starting with url, to a maximum of depth.
func Crawl(url string, depth int, fetcher Fetcher, foundURLs map[string]bool) {
	// TODO: Fetch URLs in parallel.
	// TODO: Don't fetch the same URL twice.
	// This implementation doesn't do either:
	if depth <= 0 {
		return
	}
	body, urls, err := fetcher.Fetch(url)
	if err != nil {
		fmt.Println(err)
		foundURLs[url] = false
		return
	}
	fmt.Printf("found: %s %q\n", url, body)
	foundURLs[url] = true
	for _, u := range urls {
		Crawl(u, depth-1, fetcher, foundURLs)
	}
	return
}

func main() {
	foundURLs := make(map[string]bool)
	Crawl("https://golang.org/", 4, fetcher, foundURLs)
	for url, success := range foundURLs {fmt.Println(url, success)}
}

// fakeFetcher is Fetcher that returns canned results.
type fakeFetcher map[string]*fakeResult

type fakeResult struct {
	body string
	urls []string
}

func (f fakeFetcher) Fetch(url string) (string, []string, error) {
	if res, ok := f[url]; ok {
		return res.body, res.urls, nil
	}
	return "", nil, fmt.Errorf("not found: %s", url)
}

// fetcher is a populated fakeFetcher.
var fetcher = fakeFetcher{
	"https://golang.org/": &fakeResult{
		"The Go Programming Language",
		[]string{
			"https://golang.org/pkg/",
			"https://golang.org/cmd/",
		},
	},
	"https://golang.org/pkg/": &fakeResult{
		"Packages",
		[]string{
			"https://golang.org/",
			"https://golang.org/cmd/",
			"https://golang.org/pkg/fmt/",
			"https://golang.org/pkg/os/",
		},
	},
	"https://golang.org/pkg/fmt/": &fakeResult{
		"Package fmt",
		[]string{
			"https://golang.org/",
			"https://golang.org/pkg/",
		},
	},
	"https://golang.org/pkg/os/": &fakeResult{
		"Package os",
		[]string{
			"https://golang.org/",
			"https://golang.org/pkg/",
		},
	},
}

参考:


https://tour.golang.org
https://blog.golang.org/defer-panic-and-recover
https://blog.golang.org/go-slices-usage-and-internals

503 total views, no views today

Hello, Iris Go

最近在学习 Iris 框架,首先,我们参照教程来写个 Hello World。


Install Go & Iris

brew install go
go get -u github.com/kataras/iris

Create working directory & main.go

mkdir $GOPATH/src/gowork
cd $GOPATH/src/gowork
touch main.go
package main

import (
	"github.com/kataras/iris"

	"github.com/kataras/iris/middleware/logger"
	"github.com/kataras/iris/middleware/recover"
)

func main() {
	app := iris.New()
	app.Logger().SetLevel("debug")
	// Optionally, add two built'n handlers
	// that can recover from any http-relative panics
	// and log the requests to the terminal.
	app.Use(recover.New())
	app.Use(logger.New())

	// Method:   GET
	// Resource: http://localhost:8080/hello
	app.Get("/hello", func(ctx iris.Context) {
		ctx.JSON(iris.Map{"message": "Hello, Iris Go!"})
	})

	app.Run(iris.Addr(":8080"), iris.WithoutServerError(iris.ErrServerClosed))
}

Run Web Server

go run main.go
Now listening on: http://localhost:8080 
Application started. Press CTRL+C to shut down.

参考:

https://iris-go.com/v11/start
https://golang.org/doc/code.html#GOPATH
https://github.com/iris-contrib/examples

330 total views, no views today