Skip to content

Commit fe703da

Browse files
committed
Translation of section 2.4 to Portuguese (PT_BR).
1 parent 8ac3f39 commit fe703da

File tree

6 files changed

+89
-89
lines changed

6 files changed

+89
-89
lines changed

pt-br/02.4.md

+52-52
Original file line numberDiff line numberDiff line change
@@ -2,60 +2,60 @@
22

33
## struct
44

5-
We can define new types of containers of other properties or fields in Go just like in other programming languages. For example, we can create a type called `person` to represent a person, with fields name and age. We call this kind of type a `struct`.
5+
Podemos definir novos tipos de contêineres de outras propriedades ou campos em Go exatamente como em outras linguagens de programação. Por exemplo, podemos criar um tipo chamado `person` com os campos name (nome) e age (idade) para representar uma pessoa. Chamamos este tipo de `struct` (estrutura).
66

77
type person struct {
88
name string
99
age int
1010
}
1111

12-
Look how easy it is to define a `struct`!
12+
Veja como é fácil definir uma `struct`!
1313

14-
There are two fields.
14+
Existem dois campos.
1515

16-
- `name` is a `string` used to store a person's name.
17-
- `age` is a `int` used to store a person's age.
16+
- `name` é uma `string` usada para armazenar o nome da pessoa.
17+
- `age` é um `int` usado para armazenar a idade da pessoa.
1818

19-
Let's see how to use it.
19+
Vamos ver como utilizar isto.
2020

2121
type person struct {
2222
name string
2323
age int
2424
}
2525

26-
var P person // p is person type
26+
var P person // p é do tipo person
2727

28-
P.name = "Astaxie" // assign "Astaxie" to the field 'name' of p
29-
P.age = 25 // assign 25 to field 'age' of p
30-
fmt.Printf("The person's name is %s\n", P.name) // access field 'name' of p
28+
P.name = "Astaxie" // atribui "Astaxie" para o campo 'name' de p
29+
P.age = 25 // atribui 25 para o campo 'age' de p
30+
fmt.Printf("The person's name is %s\n", P.name) // acessa o campo 'name' de p
3131

32-
There are three more ways to define a struct.
32+
Existem outras três maneiras de definir uma estrutura.
3333

34-
- Assign initial values by order
34+
- Atribuir os valores iniciais em ordem
3535

3636
P := person{"Tom", 25}
3737

38-
- Use the format `field:value` to initialize the struct without order
38+
- Usar o formato `field:value` (campo:valor) para inicializar a estrutura sem ordem
3939

4040
P := person{age:24, name:"Bob"}
4141

42-
- Define an anonymous struct, then initialize it
42+
- Definir uma estrutura anônima e então inicializar ela
4343

4444
P := struct{name string; age int}{"Amy",18}
4545
46-
Let's see a complete example.
46+
Vejamos um exemplo completo.
4747

4848
package main
4949
import "fmt"
5050

51-
// define a new type
51+
// define um novo tipo
5252
type person struct {
5353
name string
5454
age int
5555
}
5656

57-
// compare the age of two people, then return the older person and differences of age
58-
// struct is passed by value
57+
// compara a idade de duas pessoas e retorna dois valores: a pessoa mais velha e a diferença de idade
58+
// estrutura é passada por valor
5959
func Older(p1, p2 person) (person, int) {
6060
if p1.age>p2.age {
6161
return p1, p1.age-p2.age
@@ -66,13 +66,13 @@ Let's see a complete example.
6666
func main() {
6767
var tom person
6868

69-
// initialization
69+
// inicialização
7070
tom.name, tom.age = "Tom", 18
7171

72-
// initialize two values by format "field:value"
72+
// inicializa dois valores pelo formato "campo:valor"
7373
bob := person{age:25, name:"Bob"}
7474

75-
// initialize two values with order
75+
// inicializa dois valores em ordem
7676
paul := person{"Paul", 43}
7777

7878
tb_Older, tb_diff := Older(tom, bob)
@@ -86,13 +86,13 @@ Let's see a complete example.
8686
fmt.Printf("Of %s and %s, %s is older by %d years\n", bob.name, paul.name, bp_Older.name, bp_diff)
8787
}
8888

89-
### embedded fields in struct
89+
### Campos incorporados em struct
9090

91-
I've just introduced to you how to define a struct with field names and type. In fact, Go supports fields without names, but with types. We call these embedded fields.
91+
Acabei de apresentar a você como definir uma estrutura com nomes de campos e tipos. Na verdade, Go suporta campos sem nomes, mas com tipos. Chamamos esses campos de campos incorporados (embedded fields).
9292

93-
When the embedded field is a struct, all the fields in that struct will implicitly be the fields in the struct in which it has been embdedded.
93+
Quando um campo incorporado é uma estrutura, todos os campos desta estrutura serão implicitamente campos da estrutura onde ela foi incorporada.
9494

95-
Let's see one example.
95+
Vejamos um exemplo.
9696

9797
package main
9898
import "fmt"
@@ -104,43 +104,43 @@ Let's see one example.
104104
}
105105

106106
type Student struct {
107-
Human // embedded field, it means Student struct includes all fields that Human has.
107+
Human // campo incorporado, significa que a estrutura Student inclui todos os campos que Human possui
108108
specialty string
109109
}
110110

111111
func main() {
112-
// initialize a student
112+
// inicializa um estudante
113113
mark := Student{Human{"Mark", 25, 120}, "Computer Science"}
114114

115-
// access fields
115+
// acessa os campos
116116
fmt.Println("His name is ", mark.name)
117117
fmt.Println("His age is ", mark.age)
118118
fmt.Println("His weight is ", mark.weight)
119119
fmt.Println("His specialty is ", mark.specialty)
120-
// modify notes
120+
// modifica a especialidade
121121
mark.specialty = "AI"
122122
fmt.Println("Mark changed his specialty")
123123
fmt.Println("His specialty is ", mark.specialty)
124-
// modify age
124+
// modifica a idade
125125
fmt.Println("Mark become old")
126126
mark.age = 46
127127
fmt.Println("His age is", mark.age)
128-
// modify weight
128+
// modifica o peso
129129
fmt.Println("Mark is not an athlet anymore")
130130
mark.weight += 60
131131
fmt.Println("His weight is", mark.weight)
132132
}
133133

134134
![](images/2.4.student_struct.png?raw=true)
135135

136-
Figure 2.7 Inheritance in Student and Human
136+
Figure 2.7 Herança em Student e Human
137137

138-
We see that we can access the age and name fields in Student just like we can in Human. This is how embedded fields work. It's very cool, isn't it? Hold on, there's something cooler! You can even use Student to access Human in this embedded field!
138+
Vimos que podemos acessar os campos idade e nome de Student exatamente como podemos em Human. É assim que os campos incorporados funcionam. É muito legal, não é? Espere, tem algo mais legal! Você pode até usar o Student para acessar o Human neste campo incorporado!
139139

140140
mark.Human = Human{"Marcus", 55, 220}
141141
mark.Human.age -= 1
142142

143-
All the types in Go can be used as embedded fields.
143+
Todos os tipos em Go podem ser usados como campos incorporados.
144144

145145
package main
146146
import "fmt"
@@ -154,61 +154,61 @@ All the types in Go can be used as embedded fields.
154154
}
155155

156156
type Student struct {
157-
Human // struct as embedded field
158-
Skills // string slice as embedded field
159-
int // built-in type as embedded field
157+
Human // estrutura como campo incorporado
158+
Skills // string slice como campo incorporado
159+
int // tipo embutido como campo incorporado
160160
specialty string
161161
}
162162

163163
func main() {
164-
// initialize Student Jane
164+
// inicializa o Student Jane
165165
jane := Student{Human:Human{"Jane", 35, 100}, specialty:"Biology"}
166-
// access fields
166+
// acessa os campos
167167
fmt.Println("Her name is ", jane.name)
168168
fmt.Println("Her age is ", jane.age)
169169
fmt.Println("Her weight is ", jane.weight)
170170
fmt.Println("Her specialty is ", jane.specialty)
171-
// modify value of skill field
171+
// modifica o valor do campo skill
172172
jane.Skills = []string{"anatomy"}
173173
fmt.Println("Her skills are ", jane.Skills)
174174
fmt.Println("She acquired two new ones ")
175175
jane.Skills = append(jane.Skills, "physics", "golang")
176176
fmt.Println("Her skills now are ", jane.Skills)
177-
// modify embedded field
177+
// modifica o campo incorporado
178178
jane.int = 3
179179
fmt.Println("Her preferred number is", jane.int)
180180
}
181-
182-
In the above example, we can see that all types can be embedded fields and we can use functions to operate on them.
183181

184-
There is one more problem however. If Human has a field called `phone` and Student has a field with same name, what should we do?
182+
No exemplo acima, podemos ver que todos os tipos podem ser campos incorporados e podemos usar funções para operar sobre eles.
183+
184+
No entanto, existe mais um problema. Se Human possui um campo chamado `phone` e Student possui um campo com o mesmo nome, o que devemos fazer?
185185

186-
Go use a very simple way to solve it. The outer fields get upper access levels, which means when you access `student.phone`, we will get the field called phone in student, not the one in the Human struct. This feature can be simply seen as field `overload`ing.
186+
Go usa uma maneira muito simples para resolver isto. Os campos externos obtêm níveis de acesso superiores, o que significa que quando você acessa `student.phone`, você irá obter o campo chamado phone de Student, e não aquele definido na estrutura Human. Este recurso pode ser visto simplesmente como uma sobrecarga de campo (field `overload`ing).
187187

188188
package main
189189
import "fmt"
190190

191191
type Human struct {
192192
name string
193193
age int
194-
phone string // Human has phone field
194+
phone string // Human possui o campo phone
195195
}
196196

197197
type Employee struct {
198-
Human // embedded field Human
198+
Human // campo incorporado Human
199199
specialty string
200-
phone string // phone in employee
200+
phone string // phone em Employee
201201
}
202202

203203
func main() {
204204
Bob := Employee{Human{"Bob", 34, "777-444-XXXX"}, "Designer", "333-222"}
205205
fmt.Println("Bob's work phone is:", Bob.phone)
206-
// access phone field in Human
206+
// acessa o campo phone em Human
207207
fmt.Println("Bob's personal phone is:", Bob.Human.phone)
208208
}
209209

210210
## Links
211211

212-
- [Directory](preface.md)
213-
- Previous section: [Control statements and functions](02.3.md)
214-
- Next section: [Object-oriented](02.5.md)
212+
- [Sumário](preface.md)
213+
- Seção anterior: [Declarações de controle e funções](02.3.md)
214+
- Próxima seção: [Orientado a Objeto](02.5.md)

pt-br/code/src/apps/ch.2.4/compare_age/main.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
// Example code for Chapter 2.4 from "Build Web Application with Golang"
2-
// Purpose: Shows you how to pass and use structs.
1+
// Código de exemplo da seção 2.4 de "Build Web Application with Golang"
2+
// Propósito: Mostrar como utilizar estruturas em Go.
33
package main
44

55
import "fmt"
66

7-
// define a new type
7+
// define um novo tipo
88
type person struct {
99
name string
1010
age int
1111
}
1212

13-
// compare age of two people, return the older person and differences of age
14-
// struct is passed by value
13+
// compara a idade de duas pessoas e retorna dois valores: a pessoa mais velha e a diferença de idade
14+
// estrutura é passada por valor
1515
func Older(p1, p2 person) (person, int) {
1616
if p1.age > p2.age {
1717
return p1, p1.age - p2.age
@@ -22,13 +22,13 @@ func Older(p1, p2 person) (person, int) {
2222
func main() {
2323
var tom person
2424

25-
// initialization
25+
// inicialização
2626
tom.name, tom.age = "Tom", 18
2727

28-
// initialize two values by format "field:value"
28+
// inicializa dois valores pelo formato "campo:valor"
2929
bob := person{age: 25, name: "Bob"}
3030

31-
// initialize two values with order
31+
// inicializa dois valores em ordem
3232
paul := person{"Paul", 43}
3333

3434
tb_Older, tb_diff := Older(tom, bob)

pt-br/code/src/apps/ch.2.4/embedded_structs/main.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// Example code for Chapter 2.4 from "Build Web Application with Golang"
2-
// Purpose: Example of embedded fields
1+
// Código de exemplo da seção 2.4 de "Build Web Application with Golang"
2+
// Propósito: Exemplo de campos incorporados
33
package main
44

55
import "fmt"
@@ -11,28 +11,28 @@ type Human struct {
1111
}
1212

1313
type Student struct {
14-
Human // anonymous field, it means Student struct includes all fields that Human has.
14+
Human // campo anônimo, significa que a estrutura Student inclui todos os campos que Human possui.
1515
speciality string
1616
}
1717

1818
func main() {
19-
// initialize a student
19+
// inicializa um estudante
2020
mark := Student{Human{"Mark", 25, 120}, "Computer Science"}
2121

22-
// access fields
22+
// acessa os campos
2323
fmt.Println("His name is ", mark.name)
2424
fmt.Println("His age is ", mark.age)
2525
fmt.Println("His weight is ", mark.weight)
2626
fmt.Println("His speciality is ", mark.speciality)
27-
// modify notes
27+
// modifica especialidade
2828
mark.speciality = "AI"
2929
fmt.Println("Mark changed his speciality")
3030
fmt.Println("His speciality is ", mark.speciality)
31-
// modify age
31+
// modifica idade
3232
fmt.Println("Mark become old")
3333
mark.age = 46
3434
fmt.Println("His age is", mark.age)
35-
// modify weight
35+
// modifica peso
3636
fmt.Println("Mark is not an athlete any more")
3737
mark.weight += 60
3838
fmt.Println("His weight is", mark.weight)
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// Example code for Chapter 2.4 from "Build Web Application with Golang"
2-
// Purpose: Another example of embedded fields
1+
// Código de exemplo da seção 2.4 de "Build Web Application with Golang"
2+
// Propósito: Outro exemplo de campos incorporados
33
package main
44

55
import "fmt"
@@ -13,27 +13,27 @@ type Human struct {
1313
}
1414

1515
type Student struct {
16-
Human // struct as embedded field
17-
Skills // string slice as embedded field
18-
int // built-in type as embedded field
16+
Human // estrutura como campo incorporado
17+
Skills // string slice como campo incorporado
18+
int // tipo embutido como campo incorporado
1919
speciality string
2020
}
2121

2222
func main() {
23-
// initialize Student Jane
23+
// inicializa Student Jane
2424
jane := Student{Human: Human{"Jane", 35, 100}, speciality: "Biology"}
25-
// access fields
25+
// acessa os campos
2626
fmt.Println("Her name is ", jane.name)
2727
fmt.Println("Her age is ", jane.age)
2828
fmt.Println("Her weight is ", jane.weight)
2929
fmt.Println("Her speciality is ", jane.speciality)
30-
// modify value of skill field
30+
// modifica o valor do campo skill
3131
jane.Skills = []string{"anatomy"}
3232
fmt.Println("Her skills are ", jane.Skills)
3333
fmt.Println("She acquired two new ones ")
3434
jane.Skills = append(jane.Skills, "physics", "golang")
3535
fmt.Println("Her skills now are ", jane.Skills)
36-
// modify embedded field
36+
// modifica campo incorporado
3737
jane.int = 3
3838
fmt.Println("Her preferred number is", jane.int)
3939
}

0 commit comments

Comments
 (0)