Translation of section 2.4 to Portuguese (PT_BR).
This commit is contained in:
@@ -1,17 +1,17 @@
|
||||
// Example code for Chapter 2.4 from "Build Web Application with Golang"
|
||||
// Purpose: Shows you how to pass and use structs.
|
||||
// Código de exemplo da seção 2.4 de "Build Web Application with Golang"
|
||||
// Propósito: Mostrar como utilizar estruturas em Go.
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
// define a new type
|
||||
// define um novo tipo
|
||||
type person struct {
|
||||
name string
|
||||
age int
|
||||
}
|
||||
|
||||
// compare age of two people, return the older person and differences of age
|
||||
// struct is passed by value
|
||||
// compara a idade de duas pessoas e retorna dois valores: a pessoa mais velha e a diferença de idade
|
||||
// estrutura é passada por valor
|
||||
func Older(p1, p2 person) (person, int) {
|
||||
if p1.age > p2.age {
|
||||
return p1, p1.age - p2.age
|
||||
@@ -22,13 +22,13 @@ func Older(p1, p2 person) (person, int) {
|
||||
func main() {
|
||||
var tom person
|
||||
|
||||
// initialization
|
||||
// inicialização
|
||||
tom.name, tom.age = "Tom", 18
|
||||
|
||||
// initialize two values by format "field:value"
|
||||
// inicializa dois valores pelo formato "campo:valor"
|
||||
bob := person{age: 25, name: "Bob"}
|
||||
|
||||
// initialize two values with order
|
||||
// inicializa dois valores em ordem
|
||||
paul := person{"Paul", 43}
|
||||
|
||||
tb_Older, tb_diff := Older(tom, bob)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
// Example code for Chapter 2.4 from "Build Web Application with Golang"
|
||||
// Purpose: Example of embedded fields
|
||||
// Código de exemplo da seção 2.4 de "Build Web Application with Golang"
|
||||
// Propósito: Exemplo de campos incorporados
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
@@ -11,28 +11,28 @@ type Human struct {
|
||||
}
|
||||
|
||||
type Student struct {
|
||||
Human // anonymous field, it means Student struct includes all fields that Human has.
|
||||
Human // campo anônimo, significa que a estrutura Student inclui todos os campos que Human possui.
|
||||
speciality string
|
||||
}
|
||||
|
||||
func main() {
|
||||
// initialize a student
|
||||
// inicializa um estudante
|
||||
mark := Student{Human{"Mark", 25, 120}, "Computer Science"}
|
||||
|
||||
// access fields
|
||||
// acessa os campos
|
||||
fmt.Println("His name is ", mark.name)
|
||||
fmt.Println("His age is ", mark.age)
|
||||
fmt.Println("His weight is ", mark.weight)
|
||||
fmt.Println("His speciality is ", mark.speciality)
|
||||
// modify notes
|
||||
// modifica especialidade
|
||||
mark.speciality = "AI"
|
||||
fmt.Println("Mark changed his speciality")
|
||||
fmt.Println("His speciality is ", mark.speciality)
|
||||
// modify age
|
||||
// modifica idade
|
||||
fmt.Println("Mark become old")
|
||||
mark.age = 46
|
||||
fmt.Println("His age is", mark.age)
|
||||
// modify weight
|
||||
// modifica peso
|
||||
fmt.Println("Mark is not an athlete any more")
|
||||
mark.weight += 60
|
||||
fmt.Println("His weight is", mark.weight)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
// Example code for Chapter 2.4 from "Build Web Application with Golang"
|
||||
// Purpose: Another example of embedded fields
|
||||
// Código de exemplo da seção 2.4 de "Build Web Application with Golang"
|
||||
// Propósito: Outro exemplo de campos incorporados
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
@@ -13,27 +13,27 @@ type Human struct {
|
||||
}
|
||||
|
||||
type Student struct {
|
||||
Human // struct as embedded field
|
||||
Skills // string slice as embedded field
|
||||
int // built-in type as embedded field
|
||||
Human // estrutura como campo incorporado
|
||||
Skills // string slice como campo incorporado
|
||||
int // tipo embutido como campo incorporado
|
||||
speciality string
|
||||
}
|
||||
|
||||
func main() {
|
||||
// initialize Student Jane
|
||||
// inicializa Student Jane
|
||||
jane := Student{Human: Human{"Jane", 35, 100}, speciality: "Biology"}
|
||||
// access fields
|
||||
// acessa os campos
|
||||
fmt.Println("Her name is ", jane.name)
|
||||
fmt.Println("Her age is ", jane.age)
|
||||
fmt.Println("Her weight is ", jane.weight)
|
||||
fmt.Println("Her speciality is ", jane.speciality)
|
||||
// modify value of skill field
|
||||
// modifica o valor do campo skill
|
||||
jane.Skills = []string{"anatomy"}
|
||||
fmt.Println("Her skills are ", jane.Skills)
|
||||
fmt.Println("She acquired two new ones ")
|
||||
jane.Skills = append(jane.Skills, "physics", "golang")
|
||||
fmt.Println("Her skills now are ", jane.Skills)
|
||||
// modify embedded field
|
||||
// modifica campo incorporado
|
||||
jane.int = 3
|
||||
fmt.Println("Her preferred number is", jane.int)
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
// Example code for Chapter 2.4 from "Build Web Application with Golang"
|
||||
// Purpose: Shows a name conflict with a embedded field
|
||||
// Código de exemplo da seção 2.4 de "Build Web Application with Golang"
|
||||
// Propósito: Mostra um conflito de nomes com um campo incorporado
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
@@ -7,18 +7,18 @@ import "fmt"
|
||||
type Human struct {
|
||||
name string
|
||||
age int
|
||||
phone string // Human has phone field
|
||||
phone string // Human possui campo phone
|
||||
}
|
||||
|
||||
type Employee struct {
|
||||
Human // embedded field Human
|
||||
Human // campo incorporado de Human
|
||||
speciality string
|
||||
phone string // phone in employee
|
||||
phone string // phone em employee
|
||||
}
|
||||
|
||||
func main() {
|
||||
Bob := Employee{Human{"Bob", 34, "777-444-XXXX"}, "Designer", "333-222"}
|
||||
fmt.Println("Bob's work phone is:", Bob.phone)
|
||||
// access phone field in Human
|
||||
// acessa o campo phone em Human
|
||||
fmt.Println("Bob's personal phone is:", Bob.Human.phone)
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
// Example code for Chapter 2.4 from "Build Web Application with Golang"
|
||||
// Purpose: Shows different ways of creating a struct
|
||||
// Código de exemplo da seção 2.4 de "Build Web Application with Golang"
|
||||
// Propósito: Mostrar formas diferentes de criar uma estrutura
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
@@ -11,11 +11,11 @@ func show_basic_struct() {
|
||||
age int
|
||||
}
|
||||
|
||||
var P person // p is person type
|
||||
var P person // p é do tipo person
|
||||
|
||||
P.name = "Astaxie" // assign "Astaxie" to the filed 'name' of p
|
||||
P.age = 25 // assign 25 to field 'age' of p
|
||||
fmt.Printf("The person's name is %s\n", P.name) // access field 'name' of p
|
||||
P.name = "Astaxie" // atribui "Astaxie" ao campo 'name' de p
|
||||
P.age = 25 // atribui 25 ao campo 'age' de p
|
||||
fmt.Printf("The person's name is %s\n", P.name) // acessa o campo 'name' de p
|
||||
|
||||
tom := person{"Tom", 25}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user