Skip to content

Commit

Permalink
Merge pull request #29 from fabianoflorentino/development
Browse files Browse the repository at this point in the history
Development to Main
  • Loading branch information
fabianoflorentino authored Nov 21, 2024
2 parents a0d06fc + e4026c9 commit 3081578
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 6 deletions.
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
module github.com/fabianoflorentino/aprendago

go 1.22.6

require golang.org/x/text v0.20.0
go 1.23.3

require gopkg.in/yaml.v2 v2.4.0
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
golang.org/x/text v0.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug=
golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
Expand Down
91 changes: 91 additions & 0 deletions internal/aplicacoes/examples.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,31 @@ import (
"encoding/json"
"fmt"
"os"
"sort"
)

// pessoa is a struct that represents a person.
type pessoa struct {
Name string
Lastname string
Age int
}

// extractInfo is a struct that represents a json object with fields from a json file.
type extractInfo struct {
Name string `json:"Name"`
Lastname string `json:"Lastname"`
Age int `json:"Age"`
}

// carro is a struct that represents a car.
type carro struct {
Modelo string
Ano int
Potencia int
}

// UsingJsonMarshal is a function that demonstrates how to use the json package to marshal a struct.
func UsingJsonMarshal() {
p := pessoa{
Name: "Fabiano",
Expand All @@ -33,6 +44,7 @@ func UsingJsonMarshal() {
os.Stdout.Write(pessoaToJSON)
}

// UsingJsonUnmarshal is a function that demonstrates how to use the json package to unmarshal a struct.
func UsingJsonUnmarshal() {
pessoaJSON := []byte(`{"Name":"Fabiano","Lastname":"Florentino","Age":40}`)

Expand All @@ -45,6 +57,7 @@ func UsingJsonUnmarshal() {
fmt.Printf("Name: %s\nLastname: %s\nAge: %d\n", p.Name, p.Lastname, p.Age)
}

// UsingJsonEncoder is a function that demonstrates how to use the json package to encode a struct.
func UsingJsonEncoder() {
p := pessoa{
Name: "Fabiano",
Expand All @@ -54,3 +67,81 @@ func UsingJsonEncoder() {

json.NewEncoder(os.Stdout).Encode(p)
}

// UsingPackageSort is a function that demonstrates how to use the sort package to sort a slice of strings and ints.
// The sort package is a package that provides primitives for sorting slices and user-defined collections.
func UsingPackageSort() {
example_of_strings := []string{"Fabiano", "Florentino", "Aprendendo", "Go"}
example_of_ints := []int{0, 9, 5, 3, 1, 7, 8, 2, 6, 4}

fmt.Printf("Unsorted Strings: %v\n", example_of_strings)
fmt.Printf("Unsorted Ints: %v\n", example_of_ints)

usingSortSrtrings(example_of_strings)
fmt.Printf("\nSorted Strings: %v\n", example_of_strings)

usingSortInts(example_of_ints)
fmt.Printf("Sorted Ints: %v\n", example_of_ints)
}

// usingSortSrtrings is a function that demonstrates how to use the sort package to sort a slice of strings.
func usingSortSrtrings(example []string) {
sort.Strings(example)
}

// UsingSortInts is a function that demonstrates how to use the sort package to sort a slice of ints.
func usingSortInts(example []int) {
sort.Ints(example)
}

// UsingCustomSort is a function that demonstrates how to use the sort package to sort a slice of structs.
func UsingCustomSort() {
carros := []carro{
{"Fusca", 1978, 50},
{"Brasilia", 1975, 60},
{"Chevette", 1980, 70},
{"Corcel", 1979, 80},
}

fmt.Printf("Unsorted Cars: %v\n", carros)

sort.Sort(ByModel(carros))
fmt.Printf("\nSorted by Model: %v\n", carros)

sort.Sort(ByYear(carros))
fmt.Printf("Sorted by Year: %v\n", carros)

sort.Sort(ByPower(carros))
fmt.Printf("Sorted by Power: %v\n", carros)
}

type ByModel []carro
type ByYear []carro
type ByPower []carro

// ByModel implements the sort.Interface for []carro based on the Modelo field.
func (name ByModel) Len() int { return len(name) }
func (name ByModel) Less(index_i, index_j int) bool {
return name[index_i].Modelo < name[index_j].Modelo
}
func (name ByModel) Swap(index_i, index_j int) {
name[index_i], name[index_j] = name[index_j], name[index_i]
}

// ByYear implements the sort.Interface for []carro based on the Ano field.
func (year ByYear) Len() int { return len(year) }
func (year ByYear) Less(index_i, index_j int) bool {
return year[index_i].Ano < year[index_j].Ano
}
func (year ByYear) Swap(index_i, index_j int) {
year[index_i], year[index_j] = year[index_j], year[index_i]
}

// ByPower implements the sort.Interface for []carro based on the Potencia field.
func (power ByPower) Len() int { return len(power) }
func (power ByPower) Less(index_i, index_j int) bool {
return power[index_i].Potencia < power[index_j].Potencia
}
func (power ByPower) Swap(index_i, index_j int) {
power[index_i], power[index_j] = power[index_j], power[index_i]
}
7 changes: 6 additions & 1 deletion internal/aplicacoes/topics.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ func MenuAplicacoes([]string) []format.MenuOptions {
{Options: "--json-unmarshal", ExecFunc: func() { executeSections("JSON unmarshal (desornação)") }},
{Options: "--interface-writer", ExecFunc: func() { executeSections("A interface Writer") }},
{Options: "--pacote-sort", ExecFunc: func() { executeSections("O pacote sort") }},
{Options: "--pacote-sort --example", ExecFunc: func() { UsingPackageSort() }},
{Options: "--customizando-sort", ExecFunc: func() { executeSections("Customizando sort") }},
{Options: "--customizando-sort --example", ExecFunc: func() { UsingCustomSort() }},
{Options: "--bcrypt", ExecFunc: func() { executeSections("bcrypt") }},
}
}
Expand All @@ -42,9 +44,12 @@ func HelpMeAplicacoes() {
{Flag: "--documentacao-json --example --json-marshal", Description: "Exemplo de como ordenar um JSON", Width: 0},
{Flag: "--documentacao-json --example --json-unmarshal", Description: "Exemplo de como desordenar um JSON", Width: 0},
{Flag: "--documentacao-json --example --json-encoder", Description: "Exemplo de como usar o encoder JSON", Width: 0},
{Flag: "--json-marshal", Description: "Descreve o pacote json.Marshal", Width: 0},
{Flag: "--json-unmarshal", Description: "Descreve o pacote json.Unmarshal", Width: 0},
{Flag: "--interface-writer", Description: "Descreve o que é a interface Writer", Width: 0},
{Flag: "--pacote-sort", Description: "Descreve o pacote sort", Width: 0},
{Flag: "--customizando-sort", Description: "Descreve como customizar o pacote sort", Width: 0},
{Flag: "--pacote-sort --example", Description: "Exemplo de como usar o pacote sort", Width: 0},
{Flag: "--customizando-sort --example", Description: "Descreve como customizar o pacote sort", Width: 0},
{Flag: "--bcrypt", Description: "Descreve o pacote bcrypt", Width: 0},
}

Expand Down

0 comments on commit 3081578

Please sign in to comment.