請求書の各行は Line で、品名、単価 Price、数量 Qty を持っています。まず Line にその行の金額(単価 × 数量)を返すメソッド Cost() を書き、次にそれを使って請求書全体を合計する Receipt(lines) を書きましょう。請求書が空なら 0 です。
Receipt([]Line{{"pencil", 2, 5}, {"notebook", 8, 1}})→18Receipt([]Line{})→0func Receipt(lines []Line) intpackage main
type Line struct {
Name string
Price int
Qty int
}
func (l Line) Cost() int {
// ここにコードを書く:単価 × 数量を返す
return 0
}
func Receipt(lines []Line) int {
total := 0
for _, l := range lines {
// ここにコードを書く:この行の Cost() を total に足す
}
return total
}
実行すると例で試せます。よさそうなら提出しましょう。