fix: avoid global state (#126)

Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>
This commit is contained in:
Carlos Alexandro Becker 2021-09-04 13:48:46 -03:00 committed by GitHub
parent 59e5d8e2c9
commit 60ddf33992
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 20 deletions

View File

@ -95,7 +95,8 @@ do any I/O, so for the command we'll just return `nil`, which translates to "no
command." command."
```go ```go
var initialModel = model{ func main() {
initialModel := model{
// Our to-do list is just a grocery list // Our to-do list is just a grocery list
choices: []string{"Buy carrots", "Buy celery", "Buy kohlrabi"}, choices: []string{"Buy carrots", "Buy celery", "Buy kohlrabi"},
@ -103,6 +104,7 @@ var initialModel = model{
// the map like a mathematical set. The keys refer to the indexes // the map like a mathematical set. The keys refer to the indexes
// of the `choices` slice, above. // of the `choices` slice, above.
selected: make(map[int]struct{}), selected: make(map[int]struct{}),
}
} }
func (m model) Init() tea.Cmd { func (m model) Init() tea.Cmd {
@ -228,6 +230,11 @@ The last step is to simply run our program. We pass our initial model to
```go ```go
func main() { func main() {
initialModel := model{
choices: []string{"Buy carrots", "Buy celery", "Buy kohlrabi"},
selected: make(map[int]struct{}),
}
p := tea.NewProgram(initialModel) p := tea.NewProgram(initialModel)
if err := p.Start(); err != nil { if err := p.Start(); err != nil {
fmt.Printf("Alas, there's been an error: %v", err) fmt.Printf("Alas, there's been an error: %v", err)

View File

@ -59,7 +59,8 @@ do any I/O, so for the command we'll just return `nil`, which translates to "no
command." command."
```go ```go
var initialModel = model{ func main() {
initialModel := model{
// Our to-do list is just a grocery list // Our to-do list is just a grocery list
choices: []string{"Buy carrots", "Buy celery", "Buy kohlrabi"}, choices: []string{"Buy carrots", "Buy celery", "Buy kohlrabi"},
@ -67,6 +68,7 @@ var initialModel = model{
// the map like a mathematical set. The keys refer to the indexes // the map like a mathematical set. The keys refer to the indexes
// of the `choices` slice, above. // of the `choices` slice, above.
selected: make(map[int]struct{}), selected: make(map[int]struct{}),
}
} }
func (m model) Init() tea.Cmd { func (m model) Init() tea.Cmd {
@ -192,6 +194,11 @@ The last step is to simply run our program. We pass our initial model to
```go ```go
func main() { func main() {
initialModel := model{
choices: []string{"Carrots", "Celery", "Kohlrabi"},
selected: make(map[int]struct{}),
}
p := tea.NewProgram(initialModel) p := tea.NewProgram(initialModel)
if err := p.Start(); err != nil { if err := p.Start(); err != nil {
fmt.Printf("Alas, there's been an error: %v", err) fmt.Printf("Alas, there's been an error: %v", err)

View File

@ -13,11 +13,6 @@ type model struct {
selected map[int]struct{} selected map[int]struct{}
} }
var initialModel = model{
choices: []string{"Carrots", "Celery", "Kohlrabi"},
selected: make(map[int]struct{}),
}
func (m model) Init() tea.Cmd { func (m model) Init() tea.Cmd {
return nil return nil
} }
@ -72,6 +67,11 @@ func (m model) View() string {
} }
func main() { func main() {
initialModel := model{
choices: []string{"Carrots", "Celery", "Kohlrabi"},
selected: make(map[int]struct{}),
}
p := tea.NewProgram(initialModel) p := tea.NewProgram(initialModel)
if err := p.Start(); err != nil { if err := p.Start(); err != nil {
fmt.Printf("Alas, there's been an error: %v", err) fmt.Printf("Alas, there's been an error: %v", err)