Skip to content

Commit 2392e62

Browse files
authored
Merge pull request #5
feat/section17
2 parents 1a48f57 + 8cf3b01 commit 2392e62

7 files changed

Lines changed: 262 additions & 0 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@
1919

2020
# Go workspace file
2121
go.work
22+
.idea
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package main
2+
3+
import "fmt"
4+
5+
// this exercice is about data type, aka, aggregate data type, aka, complex data type
6+
7+
type Engine struct {
8+
engineType string
9+
fuelType string
10+
cylinders int
11+
}
12+
13+
func (e *Engine) Start() {
14+
fmt.Println("Engine Start")
15+
}
16+
17+
type Car struct {
18+
Engine // embedding the Engine Struct
19+
color string
20+
model string
21+
doorNumber int
22+
}
23+
24+
func (c *Car) Bip() {
25+
fmt.Println("Car Bip")
26+
}
27+
28+
func main() {
29+
car := Car{}
30+
car.Start()
31+
car.Bip()
32+
33+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package main
2+
3+
import "fmt"
4+
5+
type person struct {
6+
first string
7+
last string
8+
age int
9+
}
10+
11+
type secretAgent struct {
12+
// agent person // this way you need to use dots to access
13+
person // this way is really embedded, you just need to populate and can access inside field directly
14+
ltk bool
15+
first string // you can hava a parameter with same name that have in embedded struct param
16+
}
17+
18+
func main() {
19+
sa1 := secretAgent{
20+
person: person{
21+
first: "James",
22+
last: "Bond",
23+
age: 27,
24+
},
25+
first: "Steve vai",
26+
ltk: true,
27+
}
28+
29+
p2 := person{
30+
first: "Jenny",
31+
last: "MoneyPenny",
32+
age: 27,
33+
}
34+
35+
// anonymous struct
36+
p1 := struct {
37+
first string
38+
last string
39+
age int
40+
}{
41+
first: "James",
42+
last: "Bond",
43+
age: 32,
44+
}
45+
46+
fmt.Println(sa1)
47+
fmt.Println(p2)
48+
fmt.Println(p1)
49+
fmt.Printf("P1 type = %T\n", p1)
50+
51+
fmt.Printf("%T\t%#v\n", sa1, p2)
52+
fmt.Printf("sa1: %v\n", sa1)
53+
54+
fmt.Printf("sa1.first: %v\n", sa1.age)
55+
fmt.Printf("sa1.last: %v\n", sa1.last)
56+
fmt.Printf("sa1.age: %v\n", sa1.age)
57+
fmt.Println(sa1.first, sa1.person.first)
58+
}

src/udemy/go_ted/section17/main.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package main
2+
3+
import "fmt"
4+
5+
type person struct {
6+
first string
7+
last string
8+
age int
9+
}
10+
11+
func main() {
12+
p1 := person{
13+
first: "James",
14+
last: "Bond",
15+
age: 32,
16+
}
17+
18+
p2 := person{
19+
first: "Jenny",
20+
last: "MoneyPenny",
21+
age: 27,
22+
}
23+
24+
fmt.Println(p1)
25+
fmt.Println(p2)
26+
27+
fmt.Printf("%T\t%#v\n", p1, p2)
28+
fmt.Printf("p1: %v\n", p1)
29+
30+
fmt.Printf("p1.first: %v\n", p1.first)
31+
fmt.Printf("p1.last: %v\n", p1.last)
32+
fmt.Printf("p1.age: %v\n", p1.age)
33+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package main
2+
3+
import "fmt"
4+
5+
type engine struct {
6+
eletric bool
7+
}
8+
9+
type vehicle struct {
10+
engine engine
11+
make string
12+
model string
13+
color string
14+
doors int8
15+
}
16+
17+
func main() {
18+
v1 := vehicle{
19+
engine: engine{
20+
eletric: false,
21+
},
22+
make: "idk",
23+
model: "Ferrari Black",
24+
color: "Dark black deep blue",
25+
doors: 4,
26+
}
27+
v2 := vehicle{
28+
engine: engine{
29+
eletric: true,
30+
},
31+
make: "idk",
32+
model: "Mazda Speed",
33+
color: "Red",
34+
doors: 5,
35+
}
36+
{
37+
fmt.Println(v1, v2)
38+
fmt.Printf("V1 Specs:\neletric: %v\nMake: %v\nModel: %v\nColor: %v\nDoors: %v\n",
39+
v1.engine.eletric, v1.make, v1.model, v1.color, v1.doors)
40+
fmt.Printf("\nV2 Specs:\neletric: %v\nMake: %v\nModel: %v\nColor: %v\nDoors: %v\n",
41+
v2.engine.eletric, v2.make, v2.model, v2.color, v2.doors)
42+
}
43+
44+
// about anonymous struct again
45+
{
46+
p1 := struct {
47+
first string
48+
friends map[string]int
49+
favDrinks []string
50+
}{
51+
first: "vinicius",
52+
friends: map[string]int{
53+
"stete": 24,
54+
"lila": 4,
55+
},
56+
favDrinks: []string{"Spiced Chai", "Fruit Juice"},
57+
}
58+
fmt.Println(p1)
59+
fmt.Printf("My first name is %v\n", p1.first)
60+
for key, value := range p1.friends {
61+
fmt.Printf("My friend %v is %v years old: \n", key, value)
62+
}
63+
64+
fmt.Println("favDrinks:")
65+
for _, drink := range p1.favDrinks {
66+
fmt.Println(drink)
67+
}
68+
}
69+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package main
2+
3+
import "fmt"
4+
5+
type person struct {
6+
firstName string
7+
lastName string
8+
favIceCream []string
9+
}
10+
11+
func main() {
12+
p1 := person{
13+
firstName: "John",
14+
lastName: "Smith",
15+
favIceCream: []string{"Vanilla", "Chocolatte"},
16+
}
17+
18+
p2 := person{
19+
firstName: "Paul",
20+
lastName: "McCartney",
21+
favIceCream: []string{"Strawberry", "Mint"},
22+
}
23+
24+
{
25+
fmt.Println(p1.firstName, p1.lastName+" likes of following flavors:")
26+
for i, v := range p1.favIceCream {
27+
fmt.Printf("%v - %v\n", i+1, v)
28+
}
29+
30+
println()
31+
fmt.Println(p2.firstName, p2.lastName+" likes of following flavors:")
32+
for i, v := range p2.favIceCream {
33+
fmt.Printf("%v - %v\n", i+1, v)
34+
}
35+
}
36+
fmt.Println("----------------------")
37+
{
38+
people_store := map[string]person{
39+
p1.lastName: p1,
40+
p2.lastName: p2,
41+
}
42+
43+
fmt.Println(people_store[p1.lastName].firstName, people_store[p1.lastName].lastName)
44+
for _, value := range people_store {
45+
for _, v2 := range value.favIceCream {
46+
fmt.Println(v2)
47+
}
48+
}
49+
}
50+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
foo()
7+
foo1("xpto")
8+
}
9+
10+
// no params and no return
11+
func foo() {
12+
fmt.Println("I am from foo")
13+
}
14+
15+
// no return
16+
func foo1(str string) {
17+
fmt.Println("I am from foo", str)
18+
}

0 commit comments

Comments
 (0)