fix: avoid global state (#126)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Carlos Alexandro Becker · committed by GitHub · parent 59e5d8e · Browse files · 3 files·+34 added−20 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>
Diff stats
3 files+34 added−20 removed
- other:+34 added−20 removed
Provenance
- Commit
60ddf33- Tree
b346f7b- Author
Carlos Alexandro Becker <caarlos0@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
59e5d8e
3 files changed+34 added−20 removed
modified:tutorials/basics/main.go+5 added-5 removed
11 unmodified lines12 choices []string13 selected map[int]struct{}14 }15 16-var initialModel = model{17- choices: []string{"Carrots", "Celery", "Kohlrabi"},18- selected: make(map[int]struct{}),19-}20-16 func (m model) Init() tea.Cmd {17 return nil18 }19 46 unmodified lines66 return s67 }68 69 func main() {70+ initialModel := model{71+ choices: []string{"Carrots", "Celery", "Kohlrabi"},72+ selected: make(map[int]struct{}),73+ }74+75 p := tea.NewProgram(initialModel)76 if err := p.Start(); err != nil {77 fmt.Printf("Alas, there's been an error: %v", err)78 os.Exit(1)2 unmodified linesmodified:tutorials/basics/README.md+14 added-7 removed
57 unmodified lines58 do any I/O, so for the command we'll just return `nil`, which translates to "no59 command."60 61 ```go62+func main() {62-var initialModel = model{63- // Our to-do list is just a grocery list64- choices: []string{"Buy carrots", "Buy celery", "Buy kohlrabi"},63+ initialModel := model{64+ // Our to-do list is just a grocery list65+ choices: []string{"Buy carrots", "Buy celery", "Buy kohlrabi"},66 66- // A map which indicates which choices are selected. We're using67- // the map like a mathematical set. The keys refer to the indexes68- // of the `choices` slice, above.69- selected: make(map[int]struct{}),67+ // A map which indicates which choices are selected. We're using68+ // the map like a mathematical set. The keys refer to the indexes69+ // of the `choices` slice, above.70+ selected: make(map[int]struct{}),71+ }72 }73 74 func (m model) Init() tea.Cmd {75 // Just return `nil`, which means "no I/O right now, please."117 unmodified lines193 `tea.NewProgram` and let it rip:194 195 ```go196 func main() {197+ initialModel := model{198+ choices: []string{"Carrots", "Celery", "Kohlrabi"},199+ selected: make(map[int]struct{}),200+ }201+202 p := tea.NewProgram(initialModel)203 if err := p.Start(); err != nil {204 fmt.Printf("Alas, there's been an error: %v", err)205 os.Exit(1)40 unmodified linesmodified:README.md+15 added-8 removed
87 unmodified lines88 89 ## Initialization90 91 Next we'll define our application’s initial state. We’ll store our initial92-model in a simple variable, and then define the `Init` method. `Init` can92+model in a simple variable, and then define the `Init` method. `Init` can93 return a `Cmd` that could perform some initial I/O. For now, we don't need to94 do any I/O, so for the command we'll just return `nil`, which translates to "no95 command."96 97 ```go98+func main() {98-var initialModel = model{99- // Our to-do list is just a grocery list100- choices: []string{"Buy carrots", "Buy celery", "Buy kohlrabi"},99+ initialModel := model{100+ // Our to-do list is just a grocery list101+ choices: []string{"Buy carrots", "Buy celery", "Buy kohlrabi"},102 102- // A map which indicates which choices are selected. We're using103- // the map like a mathematical set. The keys refer to the indexes104- // of the `choices` slice, above.105- selected: make(map[int]struct{}),103+ // A map which indicates which choices are selected. We're using104+ // the map like a mathematical set. The keys refer to the indexes105+ // of the `choices` slice, above.106+ selected: make(map[int]struct{}),107+ }108 }109 110 func (m model) Init() tea.Cmd {111 // Just return `nil`, which means "no I/O right now, please."117 unmodified lines229 `tea.NewProgram` and let it rip:230 231 ```go232 func main() {233+ initialModel := model{234+ choices: []string{"Buy carrots", "Buy celery", "Buy kohlrabi"},235+ selected: make(map[int]struct{}),236+ }237+238 p := tea.NewProgram(initialModel)239 if err := p.Start(); err != nil {240 fmt.Printf("Alas, there's been an error: %v", err)241 os.Exit(1)82 unmodified lines