Skip to content

Commit ec26932

Browse files
feat: add concorrencia package with utilities for concurrency topics in Go
1 parent f92e551 commit ec26932

File tree

3 files changed

+137
-0
lines changed

3 files changed

+137
-0
lines changed

internal/concorrencia/help.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Package concorrencia provides utilities and examples related to concurrency in Go.
2+
// It includes functions and constants that demonstrate key concepts such as
3+
// concurrency vs parallelism, goroutines, waitgroups, race conditions, mutexes,
4+
// and atomic operations. This package is designed to support learning and
5+
// understanding of concurrency topics covered in Chapter 18.
6+
package concorrencia
7+
8+
import (
9+
base "github.com/fabianoflorentino/aprendago/pkg/base_content"
10+
"github.com/fabianoflorentino/aprendago/pkg/format"
11+
)
12+
13+
// Help provides a summary of concurrency-related topics covered in Chapter 18.
14+
// It creates a list of help items, each containing a flag and its corresponding description,
15+
// and then uses the format package to display the help information in a structured format.
16+
func Help() {
17+
h := []format.HelpMe{
18+
{Flag: flagConcorrenciaVsParalelismo, Description: descConcorrenciaVsParalelismo},
19+
{Flag: flagGoroutinesWaitgroups, Description: descGoroutinesWaitgroups},
20+
{Flag: flagDiscussaoCondicaoDeCorrida, Description: descDiscussaoCondicaoDeCorrida},
21+
{Flag: flagCondicaoDeCorrida, Description: descCondicaoDeCorrida},
22+
{Flag: flagMutex, Description: descMutex},
23+
{Flag: flagAtomic, Description: descAtomic},
24+
}
25+
26+
b := base.New()
27+
b.HelpMe("Capítulo 18: Concorrência", h)
28+
}

internal/concorrencia/menu.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Package concorrencia provides utilities and structures for working with
2+
// concurrency-related topics in Go. It includes functionality for creating
3+
// menus that explore various aspects of concurrency, such as the differences
4+
// between concurrency and parallelism, the use of goroutines and waitgroups,
5+
// handling race conditions, and synchronization mechanisms like mutexes and
6+
// atomic operations. This package is designed to facilitate learning and
7+
// experimentation with concurrency concepts in Go.
8+
package concorrencia
9+
10+
import (
11+
base "github.com/fabianoflorentino/aprendago/pkg/base_content"
12+
"github.com/fabianoflorentino/aprendago/pkg/format"
13+
)
14+
15+
// Menu generates a list of menu options for concurrency-related topics.
16+
// It takes a slice of strings as input and returns a slice of formatted menu options.
17+
// The function initializes a base menu object and defines flags and execution functions
18+
// for various concurrency topics such as concurrency vs parallelism, goroutines and waitgroups,
19+
// race conditions, mutexes, and atomic operations. These flags and functions are then passed
20+
// to the base menu object to create the final menu structure.
21+
func Menu([]string) []format.MenuOptions {
22+
m := base.New()
23+
24+
newFlag := []string{
25+
flagConcorrenciaVsParalelismo,
26+
flagGoroutinesWaitgroups,
27+
flagDiscussaoCondicaoDeCorrida,
28+
flagCondicaoDeCorrida,
29+
flagMutex,
30+
flagAtomic,
31+
}
32+
33+
newExecFunc := []func(){
34+
func() { m.SectionFormat(concorrenciaVsParalelismo, m.SectionFactory(rootDir)) },
35+
func() { m.SectionFormat(goroutinesWaitgroups, m.SectionFactory(rootDir)) },
36+
func() { m.SectionFormat(discussaoCondicaoDeCorrida, m.SectionFactory(rootDir)) },
37+
func() { m.SectionFormat(condicaoDeCorrida, m.SectionFactory(rootDir)) },
38+
func() { m.SectionFormat(mutex, m.SectionFactory(rootDir)) },
39+
func() { m.SectionFormat(atomic, m.SectionFactory(rootDir)) },
40+
}
41+
42+
return m.Menu(newFlag, newExecFunc)
43+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Package concorrencia provides utilities and constants for exploring concurrency-related
2+
// topics in Go. It includes definitions for topic names, flags, and descriptions
3+
// that can be used to structure and reference concurrency concepts consistently
4+
// throughout the project. This package is designed to facilitate learning and
5+
// experimentation with concurrency mechanisms such as goroutines, waitgroups, mutexes,
6+
// and atomic operations.
7+
package concorrencia
8+
9+
// rootDir represents the relative path to the directory where concurrency-related
10+
// topics are stored within the project. This constant is used to reference the
11+
// directory in a consistent manner throughout the codebase.
12+
const (
13+
rootDir = "internal/concorrencia"
14+
)
15+
16+
// The following constants define the names of various concurrency-related topics.
17+
// These constants are used to identify and reference specific sections of the
18+
// concurrency content.
19+
const (
20+
concorrenciaVsParalelismo string = "Concorrência vs Paralelismo"
21+
goroutinesWaitgroups string = "Goroutines & WaitGroups"
22+
discussaoCondicaoDeCorrida string = "Discussão: Condição de corrida"
23+
condicaoDeCorrida string = "Condição de corrida"
24+
mutex string = "Mutex"
25+
atomic string = "Atomic"
26+
)
27+
28+
// The following constants define the command-line flags that can be used to
29+
// execute specific concurrency-related topics. These flags are used to
30+
// provide a user-friendly interface for selecting and executing different
31+
// sections of the concurrency content.
32+
const (
33+
flagConcorrenciaVsParalelismo string = "--concorrencia-vs-paralelismo"
34+
flagGoroutinesWaitgroups string = "--goroutines-waitgroups"
35+
flagDiscussaoCondicaoDeCorrida string = "--discussao-condicao-de-corrida"
36+
flagCondicaoDeCorrida string = "--condicao-de-corrida"
37+
flagMutex string = "--mutex"
38+
flagAtomic string = "--atomic"
39+
)
40+
41+
// The following constants define the descriptions for each concurrency-related
42+
// topic. These descriptions provide additional context and information about
43+
// the content covered in each section. They are used to enhance the user
44+
// experience by providing clear explanations of what each topic entails.
45+
const (
46+
descConcorrenciaVsParalelismo string = "Apresenta a diferença entre concorrência e paralelismo."
47+
descGoroutinesWaitgroups string = "Apresenta o uso de goroutines e waitgroups."
48+
descDiscussaoCondicaoDeCorrida string = "Apresenta uma discussão sobre condição de corrida."
49+
descCondicaoDeCorrida string = "Apresenta o conceito de condição de corrida."
50+
descMutex string = "Apresenta o uso de mutex."
51+
descAtomic string = "Apresenta o uso de atomic."
52+
)
53+
54+
// The following slice contains the names of all concurrency-related topics.
55+
// This slice is used to iterate over the topics and execute the corresponding
56+
// sections when the user selects a specific topic.
57+
var (
58+
topics = []string{
59+
concorrenciaVsParalelismo,
60+
goroutinesWaitgroups,
61+
discussaoCondicaoDeCorrida,
62+
condicaoDeCorrida,
63+
mutex,
64+
atomic,
65+
}
66+
)

0 commit comments

Comments
 (0)