Skip to content

Commit

Permalink
fix: avoid global state
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>
  • Loading branch information
caarlos0 committed Sep 4, 2021
1 parent 59e5d8e commit 17e7ea5
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 22 deletions.
25 changes: 16 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,22 @@ type model struct {
## Initialization

Next we'll define our application’s initial state. We’ll store our initial
model in a simple variable, and then define the `Init` method. `Init` can
model in a simple variable, and then define the `Init` method. `Init` can
return a `Cmd` that could perform some initial I/O. For now, we don't need to
do any I/O, so for the command we'll just return `nil`, which translates to "no
command."

```go
var initialModel = model{
// Our to-do list is just 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{}),
func main() {
initialModel := model{
// Our to-do list is just 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{}),
}
}

func (m model) Init() tea.Cmd {
Expand Down Expand Up @@ -228,6 +230,11 @@ The last step is to simply run our program. We pass our initial model to

```go
func main() {
initialModel := model{
choices: []string{"Buy carrots", "Buy celery", "Buy kohlrabi"},
selected: make(map[int]struct{}),
}

p := tea.NewProgram(initialModel)
if err := p.Start(); err != nil {
fmt.Printf("Alas, there's been an error: %v", err)
Expand Down
23 changes: 15 additions & 8 deletions tutorials/basics/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,16 @@ do any I/O, so for the command we'll just return `nil`, which translates to "no
command."

```go
var initialModel = model{
// Our to-do list is just 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{}),
func main() {
initialModel := model{
// Our to-do list is just 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{}),
}
}

func (m model) Init() tea.Cmd {
Expand Down Expand Up @@ -192,6 +194,11 @@ The last step is to simply run our program. We pass our initial model to

```go
func main() {
initialModel := model{
choices: []string{"Carrots", "Celery", "Kohlrabi"},
selected: make(map[int]struct{}),
}

p := tea.NewProgram(initialModel)
if err := p.Start(); err != nil {
fmt.Printf("Alas, there's been an error: %v", err)
Expand Down
10 changes: 5 additions & 5 deletions tutorials/basics/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ type model struct {
selected map[int]struct{}
}

var initialModel = model{
choices: []string{"Carrots", "Celery", "Kohlrabi"},
selected: make(map[int]struct{}),
}

func (m model) Init() tea.Cmd {
return nil
}
Expand Down Expand Up @@ -72,6 +67,11 @@ func (m model) View() string {
}

func main() {
initialModel := model{
choices: []string{"Carrots", "Celery", "Kohlrabi"},
selected: make(map[int]struct{}),
}

p := tea.NewProgram(initialModel)
if err := p.Start(); err != nil {
fmt.Printf("Alas, there's been an error: %v", err)
Expand Down

0 comments on commit 17e7ea5

Please sign in to comment.