Skip to content
Myr

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

3 files+34 added−20 removed

modified:tutorials/basics/main.go+5 added-5 removed
11 unmodified lines
12 choices []string13 selected map[int]struct{}14}1516var initialModel = model{17 choices: []string{"Carrots", "Celery", "Kohlrabi"},18 selected: make(map[int]struct{}),19}2016func (m model) Init() tea.Cmd {17 return nil18}19
46 unmodified lines
66 return s67}6869func main() {70 initialModel := model{71 choices: []string{"Carrots", "Celery", "Kohlrabi"},72 selected: make(map[int]struct{}),73 }7475 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 lines
modified:tutorials/basics/README.md+14 added-7 removed
57 unmodified lines
58do any I/O, so for the command we'll just return `nil`, which translates to "no59command."6061```go62func main() {62var 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"},6666 // 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}7374func (m model) Init() tea.Cmd {75 // Just return `nil`, which means "no I/O right now, please."
117 unmodified lines
193`tea.NewProgram` and let it rip:194195```go196func main() {197 initialModel := model{198 choices: []string{"Carrots", "Celery", "Kohlrabi"},199 selected: make(map[int]struct{}),200 }201202 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 lines
modified:README.md+15 added-8 removed
87 unmodified lines
8889## Initialization9091Next we'll define our application’s initial state. We’ll store our initial92model in a simple variable, and then define the `Init` method. `Init` can92model in a simple variable, and then define the `Init` method. `Init` can93return a `Cmd` that could perform some initial I/O. For now, we don't need to94do any I/O, so for the command we'll just return `nil`, which translates to "no95command."9697```go98func main() {98var 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"},102102 // 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}109110func (m model) Init() tea.Cmd {111 // Just return `nil`, which means "no I/O right now, please."
117 unmodified lines
229`tea.NewProgram` and let it rip:230231```go232func main() {233 initialModel := model{234 choices: []string{"Buy carrots", "Buy celery", "Buy kohlrabi"},235 selected: make(map[int]struct{}),236 }237238 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