Last active
April 24, 2021 14:51
-
-
Save royling/ebcef895542bf78eabb0ff16b43c09ad to your computer and use it in GitHub Desktop.
go tour exercises
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
// fibonacci is a function that returns | |
// a function that returns an int. | |
func fibonacci() func() int { | |
a, b := 0, 1 | |
return func() int { | |
i := a | |
a, b = b, a+b | |
return i | |
} | |
} | |
func main() { | |
f := fibonacci() | |
for i := 0; i < 10; i++ { | |
fmt.Println(f()) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "golang.org/x/tour/reader" | |
type MyReader struct{} | |
// TODO: Add a Read([]byte) (int, error) method to MyReader. | |
func (r MyReader) Read(b []byte) (int, error) { | |
b[0] = 'A' | |
return 1, nil | |
} | |
func main() { | |
reader.Validate(MyReader{}) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"strings" | |
"golang.org/x/tour/wc" | |
) | |
func WordCount(s string) map[string]int { | |
m := make(map[string]int) | |
for _, w := range strings.Fields(s) { | |
//if _, present := m[w]; !present { | |
// m[w] = 0 | |
//} | |
m[w] += 1 | |
} | |
return m | |
} | |
func main() { | |
wc.Test(WordCount) | |
} | |
/* | |
var m2 = map[int]string{ | |
1: "a", | |
2: "b", | |
} | |
// Pointer as key type | |
var m3 = map[*Vertex]string { | |
&Vertex{40.68433, -74.39967,}: "Bell", | |
&Vertex{37.42202, -122.08408,}: "Google", | |
nil: "nil", | |
} | |
// Pointer as value type | |
var m4 = map[string]*Vertex { | |
"Bell Labs": &Vertex{40.68433, -74.39967}, | |
"Google": &Vertex{37.42202, -122.08408}, | |
} | |
// Cannot omit type name `Vertex` if pointer is used as key/value type! | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"io" | |
"os" | |
"strings" | |
) | |
type rot13Reader struct { | |
r io.Reader | |
} | |
func subst(b byte) byte { | |
var ( | |
alpha bool | |
start byte | |
) | |
switch { | |
case b >= 'a' && b <= 'z': | |
alpha = true | |
start = 'a' | |
case b >= 'A' && b <= 'Z': | |
alpha = true | |
start = 'A' | |
} | |
if alpha { | |
return (b+13-start)%26 + start | |
} | |
return b | |
} | |
func (rot13 *rot13Reader) Read(b []byte) (int, error) { | |
n, err := rot13.r.Read(b) | |
if err == io.EOF { | |
return 0, err | |
} | |
for i, c := range b { | |
b[i] = subst(c) | |
} | |
return n, nil | |
} | |
func main() { | |
s := strings.NewReader("Lbh penpxrq gur pbqr!") | |
r := rot13Reader{s} | |
io.Copy(os.Stdout, &r) | |
// You cracked the code! | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "golang.org/x/tour/pic" | |
type Selector func(int, int) int | |
type Renderer func(int, int) [][]uint8 | |
func PicSelector(f Selector) Renderer { | |
return func(dx, dy int) [][]uint8 { | |
var res [][]uint8 | |
for y := 0; y < dy; y++ { | |
var e []uint8 | |
for x := 0; x < dx; x++ { | |
v := uint8(f(x, y)) | |
e = append(e, v) | |
} | |
res = append(res, e) | |
} | |
return res | |
} | |
} | |
func main() { | |
pic.Show(PicSelector(func(x, y int) int { | |
return (x + y) * (x - y) | |
})) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment