Skip to content
This repository has been archived by the owner on Sep 29, 2022. It is now read-only.

Latest commit

 

History

History
62 lines (48 loc) · 1.5 KB

storing-custom-data.md

File metadata and controls

62 lines (48 loc) · 1.5 KB

ears Storing Custom Data

A store can manage any concrete type, including custom structs. Let's define a bunny type with a few fields and a String method.

type bunny struct {
    name       string
    fluffiness float64
    activities []string
}

func (b bunny) String() string {
    fluffy := "fluffy"
    if b.fluffiness < 0.5 {
        fluffy = "not fluffy"
    }
    return fmt.Sprintf("%s is %s and likes %v", b.name, fluffy, b.activities)
}

Now we create a bunny and add it to our store.

s := store.New()

peter := store.NewVar(s, bunny{
    name:       "Peter Rabbit",
    fluffiness: 0.52,
    activities: []string{
        "stealing and eating vegetables",
        "losing his jacket and shoes",
    },
})

If we retrieve the value of peter from our store and print it, we should get the results of the bunny.String method. Note that peter.Get returns a value of type bunny, so Go knows to call the appropriate String method when we pass it to fmt.Println.

fmt.Println(peter.Get(s))

Run the source above and you should see an educational message about this Peter Rabbit fellow.

Exercises

  • Add another bunny to the store and call its String method directly.
  • Add another method to the bunny type. Retrieve peter from the store and call this new method.

Previous | Next | Home