Created
June 14, 2018 12:43
-
-
Save jjuliano/fcbf91a2986fcb141c377d4cf4838d34 to your computer and use it in GitHub Desktop.
This file contains hidden or 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" | |
type basket []produce | |
type produce struct { | |
name string | |
flavour string | |
kind string | |
} | |
func (p *basket) add_item(entry produce) { | |
*p = append(*p, entry) | |
fmt.Printf("Entry %s created!\n", entry.name) | |
} | |
func (p basket) change_item(name string, entry produce) { | |
for key, val := range p { | |
if val.name == name { | |
p[key] = entry | |
} | |
} | |
fmt.Printf("Item %s changed!\n", name) | |
} | |
func (p basket) items() { | |
fmt.Println("") | |
fmt.Printf("There are %d number of items in the basket\n", len(p)) | |
fmt.Println("") | |
for _, val := range p { | |
fmt.Printf("Name: %s\nFlavour: %s\nKind: %s\n", val.name, val.flavour, val.kind) | |
fmt.Println("") | |
} | |
} | |
func main() { | |
basket := new(basket) | |
basket.add_item( | |
produce{ | |
name: "apple", | |
flavour: "It's a little sour and bitter, but mostly sweet, not at all salty, very juicy in general.", | |
kind: "fruit", | |
}, | |
) | |
basket.add_item( | |
produce{ | |
name: "carrot", | |
flavour: "", | |
kind: "veggies", | |
}, | |
) | |
basket.change_item("carrot", | |
produce{ | |
name: "cucumber", | |
flavour: "Slightly bitter with a mild melon aroma and planty flavor.", | |
kind: "veggies", | |
}, | |
) | |
basket.items() | |
} | |
/* | |
Entry apple created! | |
Entry carrot created! | |
Item carrot changed! | |
There are 2 number of items in the basket | |
Name: apple | |
Flavour: It's a little sour and bitter, but mostly sweet, not at all salty, very juicy in general. | |
Kind: fruit | |
Name: cucumber | |
Flavour: Slightly bitter with a mild melon aroma and planty flavor. | |
Kind: veggies | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment