en Home Smtp Sometimes I fall in love with code jgdb Encode/Decode Golang data to Gob files GoWatch Cfg File Access and Create Time Go Snippets Ping Citadel vCard Sync Your Everyday Go Tools Cases Conv Go jkl Encode/Decode between struct and byte slice Crypt HDKeys Wordlist HDKeys Recover

A Gopher named Gophy

Learn go with a little coding story

If you are just starting out with Go, head over to the Go Playground and concentrate on figuring out how my little Go Story works. It's about a Gopher, named Gophy who joint the Gophers but lost his identity in the process.
Once you get how he gets his identity back you'll be golden.

Here is the code:

package main

import "fmt"

type I interface{}

var gophers map[uint]I = make(map[uint]I)

type gopher struct {
    Name string
}

func main() {

    g := AddToGophers("Goghy")

    fmt.Printf("Hello, %s\n", g.Name)
    fmt.Printf("Now %s is a %T, %s\n", g.Name, gophers[1], gophers[1])

    gg := GetGopher(1)
    fmt.Printf("Bye, %s\n", gg.Name)
}

func GetGopher(i uint) *gopher {
    g := gophers[i]
    // I wont my gopher identity back
    return g.(*gopher)
}

func AddToGophers(n string) gopher {
    g := new(gopher)
    g.Name = n
    gophers[1] = g
    return *g
}

Tags: code go


First Created: 2015-01-28T19:54:59Z
Last Modified: 2026-04-12 14:52