Finished translation of Chapter 1 in pt-br

This commit is contained in:
James Miranda
2016-09-23 17:33:18 -03:00
parent 2b65ffe75a
commit bbbfb9ff8c
269 changed files with 16105 additions and 1 deletions

View File

@@ -0,0 +1,43 @@
// Example code for Chapter 2.4 from "Build Web Application with Golang"
// Purpose: Shows you how to pass and use structs.
package main
import "fmt"
// define a new type
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
func Older(p1, p2 person) (person, int) {
if p1.age > p2.age {
return p1, p1.age - p2.age
}
return p2, p2.age - p1.age
}
func main() {
var tom person
// initialization
tom.name, tom.age = "Tom", 18
// initialize two values by format "field:value"
bob := person{age: 25, name: "Bob"}
// initialize two values with order
paul := person{"Paul", 43}
tb_Older, tb_diff := Older(tom, bob)
tp_Older, tp_diff := Older(tom, paul)
bp_Older, bp_diff := Older(bob, paul)
fmt.Printf("Of %s and %s, %s is older by %d years\n", tom.name, bob.name, tb_Older.name, tb_diff)
fmt.Printf("Of %s and %s, %s is older by %d years\n", tom.name, paul.name, tp_Older.name, tp_diff)
fmt.Printf("Of %s and %s, %s is older by %d years\n", bob.name, paul.name, bp_Older.name, bp_diff)
}

View File

@@ -0,0 +1,39 @@
// Example code for Chapter 2.4 from "Build Web Application with Golang"
// Purpose: Example of embedded fields
package main
import "fmt"
type Human struct {
name string
age int
weight int
}
type Student struct {
Human // anonymous field, it means Student struct includes all fields that Human has.
speciality string
}
func main() {
// initialize a student
mark := Student{Human{"Mark", 25, 120}, "Computer Science"}
// access fields
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
mark.speciality = "AI"
fmt.Println("Mark changed his speciality")
fmt.Println("His speciality is ", mark.speciality)
// modify age
fmt.Println("Mark become old")
mark.age = 46
fmt.Println("His age is", mark.age)
// modify weight
fmt.Println("Mark is not an athlete any more")
mark.weight += 60
fmt.Println("His weight is", mark.weight)
}

View File

@@ -0,0 +1,39 @@
// Example code for Chapter 2.4 from "Build Web Application with Golang"
// Purpose: Another example of embedded fields
package main
import "fmt"
type Skills []string
type Human struct {
name string
age int
weight int
}
type Student struct {
Human // struct as embedded field
Skills // string slice as embedded field
int // built-in type as embedded field
speciality string
}
func main() {
// initialize Student Jane
jane := Student{Human: Human{"Jane", 35, 100}, speciality: "Biology"}
// access fields
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
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
jane.int = 3
fmt.Println("Her preferred number is", jane.int)
}

View File

@@ -0,0 +1,24 @@
// Example code for Chapter 2.4 from "Build Web Application with Golang"
// Purpose: Shows a name conflict with a embedded field
package main
import "fmt"
type Human struct {
name string
age int
phone string // Human has phone field
}
type Employee struct {
Human // embedded field Human
speciality string
phone string // phone in 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
fmt.Println("Bob's personal phone is:", Bob.Human.phone)
}

View File

@@ -0,0 +1,39 @@
// Example code for Chapter 2.4 from "Build Web Application with Golang"
// Purpose: Shows different ways of creating a struct
package main
import "fmt"
func show_basic_struct() {
fmt.Println("\nshow_basic_struct()")
type person struct {
name string
age int
}
var P person // p is person type
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
tom := person{"Tom", 25}
bob := person{age: 24, name: "Bob"}
fmt.Printf("tom = %+v\n", tom)
fmt.Printf("bob = %#v\n", bob)
}
func show_anonymous_struct() {
fmt.Println("\nshow_anonymous_struct()")
fmt.Printf("Anonymous struct = %#v\n", struct {
name string
count int
}{
"counter", 1,
})
}
func main() {
show_basic_struct()
show_anonymous_struct()
}