Creating an inventory

(Steve) #1


I am currently reimplementing things I wrote in Perl to get some more exposure to Go.

One of the programs creates a little inventory at runtime which looks like this:

foo.rpm = 23
bar.rpm = 42

In Perl I used a Hash to implement it and in Go the logical counterpart (for me) seems to be map[string]int.

However, I was wondering is there is a more elegant way to do this. I want to be able to add items to this inventory (e.g. blarb.rpm = 123) as the program runs and I also want to do lookups (find number for foo.rpm).

What do you think is the most fitting data/function structure to implement such a thing?

Cheers, Lordy

(Yamil Bracho) #2

Consider a struct to define the item and an slide . Say

type Item struct {
Name string
RPM int
type Items []Item

(Johan Dahl) #3

The map is a really good fit in go too