Skip to content

Instantly share code, notes, and snippets.

@royling
Last active April 24, 2021 14:51
Show Gist options
  • Save royling/ebcef895542bf78eabb0ff16b43c09ad to your computer and use it in GitHub Desktop.
Save royling/ebcef895542bf78eabb0ff16b43c09ad to your computer and use it in GitHub Desktop.

Revisions

  1. royling revised this gist Apr 24, 2021. 1 changed file with 49 additions and 0 deletions.
    49 changes: 49 additions & 0 deletions rot13_reader.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,49 @@
    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!
    }
  2. royling revised this gist Apr 24, 2021. 1 changed file with 15 additions and 0 deletions.
    15 changes: 15 additions & 0 deletions io_reader.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    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{})
    }
  3. royling revised this gist Apr 24, 2021. 1 changed file with 21 additions and 0 deletions.
    21 changes: 21 additions & 0 deletions fibonacci.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    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())
    }
    }
  4. royling revised this gist Apr 24, 2021. 1 changed file with 22 additions and 0 deletions.
    22 changes: 22 additions & 0 deletions map.go
    Original file line number Diff line number Diff line change
    @@ -21,3 +21,25 @@ func WordCount(s string) map[string]int {
    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!
    */
  5. royling revised this gist Apr 24, 2021. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions map.go
    Original file line number Diff line number Diff line change
    @@ -9,9 +9,9 @@ import (
    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
    }
    //if _, present := m[w]; !present {
    // m[w] = 0
    //}

    m[w] += 1
    }
  6. royling revised this gist Apr 24, 2021. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions map.go
    Original file line number Diff line number Diff line change
    @@ -9,10 +9,10 @@ import (
    func WordCount(s string) map[string]int {
    m := make(map[string]int)
    for _, w := range strings.Fields(s) {
    _, present := m[w]
    if !present {
    if _, present := m[w]; !present {
    m[w] = 0
    }

    m[w] += 1
    }
    return m
  7. royling revised this gist Apr 24, 2021. 1 changed file with 23 additions and 0 deletions.
    23 changes: 23 additions & 0 deletions map.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    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) {
    _, present := m[w]
    if !present {
    m[w] = 0
    }
    m[w] += 1
    }
    return m
    }

    func main() {
    wc.Test(WordCount)
    }
  8. royling renamed this gist Apr 24, 2021. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  9. royling created this gist Apr 23, 2021.
    31 changes: 31 additions & 0 deletions gotour_slice.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    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)
    }))

    }