From 1c561f66adab406a4906ede06de3cc29f8a7f2d4 Mon Sep 17 00:00:00 2001 From: rumaks-xyz Date: Fri, 1 Nov 2024 22:37:11 +0300 Subject: [PATCH 1/3] Change in the tutorial from a map to a slice --- tutorials/basics/README.md | 21 +++++++-------------- tutorials/basics/main.go | 17 +++++------------ 2 files changed, 12 insertions(+), 26 deletions(-) diff --git a/tutorials/basics/README.md b/tutorials/basics/README.md index 8e1a26d4c2..3baa75a484 100644 --- a/tutorials/basics/README.md +++ b/tutorials/basics/README.md @@ -45,9 +45,9 @@ It can be any type, but a `struct` usually makes the most sense. ```go type model struct { - choices []string // items on the to-do list - cursor int // which to-do list item our cursor is pointing at - selected map[int]struct{} // which to-do items are selected + choices []string // items on the to-do list + cursor int // which to-do list item our cursor is pointing at + selected []bool // which to-do items are selected } ``` @@ -63,10 +63,8 @@ func initialModel() model { // Our to-do list is a grocery list choices: []string{"Buy carrots", "Buy celery", "Buy kohlrabi"}, - // A map which indicates which choices are selected. We're using - // the map like a mathematical set. The keys refer to the indexes - // of the `choices` slice, above. - selected: make(map[int]struct{}), + // A bool slice which indicates which choices are selected. + selected: []bool{false, false, false}, } } ``` @@ -131,12 +129,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { // The "enter" key and the spacebar (a literal space) toggle // the selected state for the item that the cursor is pointing at. case "enter", " ": - _, ok := m.selected[m.cursor] - if ok { - delete(m.selected, m.cursor) - } else { - m.selected[m.cursor] = struct{}{} - } + m.selected[m.cursor] = !m.selected[m.cursor] } } @@ -176,7 +169,7 @@ func (m model) View() string { // Is this choice selected? checked := " " // not selected - if _, ok := m.selected[i]; ok { + if m.selected[i] { checked = "x" // selected! } diff --git a/tutorials/basics/main.go b/tutorials/basics/main.go index 71cef0a828..ef9e7bf474 100644 --- a/tutorials/basics/main.go +++ b/tutorials/basics/main.go @@ -10,17 +10,15 @@ import ( type model struct { cursor int choices []string - selected map[int]struct{} + selected []bool } func initialModel() model { return model{ choices: []string{"Buy carrots", "Buy celery", "Buy kohlrabi"}, - // A map which indicates which choices are selected. We're using - // the map like a mathematical set. The keys refer to the indexes - // of the `choices` slice, above. - selected: make(map[int]struct{}), + // A bool slice which indicates which choices are selected. + selected: []bool{false, false, false}, } } @@ -43,12 +41,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.cursor++ } case "enter", " ": - _, ok := m.selected[m.cursor] - if ok { - delete(m.selected, m.cursor) - } else { - m.selected[m.cursor] = struct{}{} - } + m.selected[m.cursor] = !m.selected[m.cursor] } } @@ -65,7 +58,7 @@ func (m model) View() string { } checked := " " - if _, ok := m.selected[i]; ok { + if m.selected[i] { checked = "x" } From 97b5d4730c8d1eeb95e3596c93eeeced3f25f7e1 Mon Sep 17 00:00:00 2001 From: rumaks-xyz Date: Fri, 1 Nov 2024 22:43:30 +0300 Subject: [PATCH 2/3] Slightly improve initialization in the tutorial --- tutorials/basics/README.md | 5 +++-- tutorials/basics/main.go | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/tutorials/basics/README.md b/tutorials/basics/README.md index 3baa75a484..731bca0dce 100644 --- a/tutorials/basics/README.md +++ b/tutorials/basics/README.md @@ -59,12 +59,13 @@ the initial model as a variable elsewhere, too. ```go func initialModel() model { + choices := []string{"Buy carrots", "Buy celery", "Buy kohlrabi"} return model{ // Our to-do list is a grocery list - choices: []string{"Buy carrots", "Buy celery", "Buy kohlrabi"}, + choices: choices, // A bool slice which indicates which choices are selected. - selected: []bool{false, false, false}, + selected: make([]bool, len(choices)), } } ``` diff --git a/tutorials/basics/main.go b/tutorials/basics/main.go index ef9e7bf474..3ce512e2c4 100644 --- a/tutorials/basics/main.go +++ b/tutorials/basics/main.go @@ -14,11 +14,12 @@ type model struct { } func initialModel() model { + choices := []string{"Buy carrots", "Buy celery", "Buy kohlrabi"} return model{ - choices: []string{"Buy carrots", "Buy celery", "Buy kohlrabi"}, + choices: choices, // A bool slice which indicates which choices are selected. - selected: []bool{false, false, false}, + selected: make([]bool, len(choices)), } } From adff03e50e51e04df52868d2102adb8a04558e0a Mon Sep 17 00:00:00 2001 From: rumaks-xyz Date: Fri, 1 Nov 2024 22:53:51 +0300 Subject: [PATCH 3/3] Move a comment to a more appropriate place --- tutorials/basics/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tutorials/basics/README.md b/tutorials/basics/README.md index 731bca0dce..a5317bd093 100644 --- a/tutorials/basics/README.md +++ b/tutorials/basics/README.md @@ -59,9 +59,9 @@ the initial model as a variable elsewhere, too. ```go func initialModel() model { + // Our to-do list is a grocery list choices := []string{"Buy carrots", "Buy celery", "Buy kohlrabi"} return model{ - // Our to-do list is a grocery list choices: choices, // A bool slice which indicates which choices are selected.