๐ŸŽ“ A1Learner
๐Ÿ”
0 of 38 lessons completed

Go Project: Shopping Basket

The Shopping Basket project stores items in a named slice type. It adds up a receipt and prints a sorted summary.

One Item

The Item struct holds a name, a price, and a quantity. One line of the receipt comes from its fields.

package main

import "fmt"

type Item struct {
	Name  string
	Price int
	Qty   int
}

func main() {
	item := Item{Name: "Pen", Price: 3, Qty: 2}
	fmt.Println(item.Name, item.Qty, item.Price*item.Qty)
}

Add an Item

Add appends an item to the basket. The basket then holds one item.

package main

import "fmt"

type Item struct {
	Name  string
	Price int
	Qty   int
}

type Basket []Item

func (b *Basket) Add(item Item) {
	*b = append(*b, item)
}

func main() {
	var basket Basket
	basket.Add(Item{Name: "Pen", Price: 3, Qty: 2})
	fmt.Println(len(basket))
	fmt.Println(basket[0].Name)
}

Total the Basket

Total adds price times quantity for each item. Two items give a total of 18.

package main

import "fmt"

type Item struct {
	Name  string
	Price int
	Qty   int
}

type Basket []Item

func (b *Basket) Add(item Item) {
	*b = append(*b, item)
}

func (b Basket) Total() int {
	total := 0
	for _, item := range b {
		total += item.Price * item.Qty
	}
	return total
}

func main() {
	var basket Basket
	basket.Add(Item{Name: "Pen", Price: 3, Qty: 2})
	basket.Add(Item{Name: "Book", Price: 12, Qty: 1})
	fmt.Println(basket.Total())
}

Example

package main

import (
	"fmt"
	"slices"
	"strings"
)

type Item struct {
	Name  string
	Price int
	Qty   int
}

type Basket []Item

func (b *Basket) Add(item Item) {
	*b = append(*b, item)
}

func (b Basket) Total() int {
	total := 0
	for _, item := range b {
		total += item.Price * item.Qty
	}
	return total
}

func main() {
	var basket Basket
	basket.Add(Item{Name: "Pen", Price: 3, Qty: 2})
	basket.Add(Item{Name: "Book", Price: 12, Qty: 1})
	basket.Add(Item{Name: "Mug", Price: 8, Qty: 1})
	count := 0
	names := []string{}
	for _, item := range basket {
		fmt.Println(item.Name, item.Qty, item.Price*item.Qty)
		count += item.Qty
		names = append(names, item.Name)
	}
	fmt.Println("Items:", count)
	fmt.Println("Total:", basket.Total())
	slices.Sort(names)
	fmt.Println(strings.Join(names, ", "))
}

Key points

  • Item holds the name, price, and quantity of one product.
  • Basket is a named slice type that holds items.
  • Add uses a pointer receiver, so the appended item stays in the basket.
  • Total sums price times quantity for every item.
  • The receipt prints items in the order they were added.
  • The summary sorts a separate names slice, so the basket order stays unchanged.

In this example, Add puts the Pen, the Book, and the Mug into the basket in that order. The loop prints one receipt line per item with its line total. The loop also counts the quantities, which add up to 4, and collects the names. Total adds the line totals into 26. The names are sorted and joined for the last line.

Try it Yourself

Edit the code, then press Run.

Running is simulated ยท Go Playground

Exercise

Why does Add use a pointer receiver?