Skip to content

Commit 6b0f5d6

Browse files
authored
Merge pull request #6 from 0bvim/feat/section19
feat/section19
2 parents 2392e62 + 166cfa9 commit 6b0f5d6

17 files changed

+535
-2
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
// not anonymous
7+
foo()
8+
9+
// anonymous
10+
func() {
11+
fmt.Println("anonymous function")
12+
}()
13+
14+
// anonymous function with param
15+
func(s string) {
16+
fmt.Println("Anon func showing my name", s)
17+
}("Nivi")
18+
19+
// can assign it to a variable too
20+
x := func(s string) {
21+
fmt.Println("Anon func showing my name to a func assigned to a variable", s)
22+
}
23+
x("Thompson")
24+
}
25+
26+
func foo() {
27+
fmt.Println("foo")
28+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"strconv"
7+
)
8+
9+
// link of example https://www.alexedwards.net/blog/interfaces-explained
10+
11+
// this is my implementation of a method that satisfy
12+
// String() string (fmt.Stringer()) interface
13+
type Item struct {
14+
Type string
15+
Name string
16+
}
17+
18+
func (it Item) String() string {
19+
return fmt.Sprintf("Type: %s - Name: %s", it.Type, it.Name)
20+
}
21+
22+
// Declare a Book type which satisfies the fmt.Stringer interface.
23+
type Book struct {
24+
Title string
25+
Author string
26+
}
27+
28+
func (b Book) String() string {
29+
return fmt.Sprintf("Book: %s - %s", b.Title, b.Author)
30+
}
31+
32+
// Declare a Count type which satisfies the fmt.Stringer interface.
33+
type Count int
34+
35+
func (c Count) String() string {
36+
return strconv.Itoa(int(c))
37+
}
38+
39+
// Declare a writeLog function which takes any object that satisfies
40+
// the fmt.Stringer interface.
41+
func WriteLog(s fmt.Stringer) {
42+
log.Println(s.String())
43+
}
44+
45+
func main() {
46+
// Initialize a Count object and pass it to WriteLog().
47+
book := Book{"Alice in Wonderland", "Lewis Carrol"}
48+
WriteLog(book)
49+
50+
// Initialize a Count object and pass it to WriteLog().
51+
count := Count(3)
52+
WriteLog(count)
53+
54+
item := Item{
55+
Type: "Eletronic",
56+
Name: "Notebook",
57+
}
58+
WriteLog(item)
59+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package main
2+
3+
func main() {
4+
println(doMath(41, 1, add))
5+
println(doMath(43, 1, subtract))
6+
println(doMath(21, 2, multiply))
7+
println(doMath(84, 2, division))
8+
}
9+
10+
// callback function are like function that receive a function pointer as param in 'c'
11+
func doMath(a, b int, f func(int, int) int) int {
12+
return f(a, b)
13+
}
14+
15+
func add(a, b int) int {
16+
return a + b
17+
}
18+
19+
func subtract(a, b int) int {
20+
return a - b
21+
}
22+
23+
func division(a, b int) int {
24+
return a / b
25+
}
26+
27+
func multiply(a, b int) int {
28+
return a * b
29+
}

src/udemy/go_ted/section19/closure.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
f := incrementor()
7+
fmt.Printf("value: %v\tMem: %v\n", f(), &f)
8+
fmt.Printf("value: %v\tMem: %v\n", f(), &f)
9+
fmt.Printf("value: %v\tMem: %v\n", f(), &f)
10+
11+
println("--------------")
12+
13+
// here, like in 'f' var above the function assigned to the 'g' variable will
14+
// be in the same memory place.
15+
// so when you call, it will have the same value stored into deeper variable 'x'
16+
g := incrementor()
17+
fmt.Printf("value: %v\tMem: %v\n", g(), &g)
18+
fmt.Printf("value: %v\tMem: %v\n", g(), &g)
19+
fmt.Printf("value: %v\tMem: %v\n", g(), &g)
20+
21+
println("--------------")
22+
23+
// if you don't assign to a variable it will return initial state of variable 'x'
24+
// because it's stored in a different place in memory
25+
fmt.Printf("Mem: %v\n", incrementor())
26+
fmt.Printf("Mem: %v\n", incrementor())
27+
fmt.Printf("Mem: %v\n", incrementor())
28+
}
29+
30+
func incrementor() func() int {
31+
x := 0
32+
return func() int {
33+
x++
34+
return x
35+
}
36+
}

src/udemy/go_ted/section19/defer.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
// when we use defer (like 'adiar' in pt_br) it's like delay when the action of function will happens
7+
{
8+
foo()
9+
bar()
10+
}
11+
// in this case foo will be printed after bar
12+
{
13+
defer foo()
14+
bar()
15+
}
16+
}
17+
18+
// func prototype
19+
// func (r receiver) identifier(p parameter(s)) (r return(s)) { <code? }
20+
21+
func foo() {
22+
fmt.Println("foo")
23+
}
24+
25+
func bar() {
26+
fmt.Println("bar")
27+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package main
2+
3+
import . "fmt"
4+
5+
func main() {
6+
x := foo()
7+
Println(x)
8+
9+
y := bar()
10+
Println(y())
11+
Printf("%T\n", foo())
12+
Printf("%T\n", bar())
13+
Printf("%T\n", y)
14+
}
15+
16+
func foo() int {
17+
return 42
18+
}
19+
20+
func bar() func() int {
21+
return func() int {
22+
return 43
23+
}
24+
}
Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,64 @@
11
package main
22

3-
import "fmt"
3+
import (
4+
"errors"
5+
"fmt"
6+
"log"
7+
)
48

59
func main() {
610
foo()
711
foo1("xpto")
12+
fmt.Println(foo2("I want"))
13+
rtn, err := foo3("I want", " more")
14+
if err != nil {
15+
log.Fatalf("error while calling foo3: %v", err)
16+
}
17+
fmt.Println(rtn)
18+
fmt.Println(dogYears("Vinicius", 31))
19+
fmt.Println(sum(1, 2, 3, 4, 5, 6, 7))
20+
21+
// unfurling a slice
22+
x := []int{1, 2, 3, 4, 5}
23+
fmt.Println(sum(x...))
24+
825
}
926

1027
// no params and no return
1128
func foo() {
1229
fmt.Println("I am from foo")
1330
}
1431

15-
// no return
32+
// 1 param no return
1633
func foo1(str string) {
1734
fmt.Println("I am from foo", str)
1835
}
36+
37+
// 1 param and 1 return
38+
func foo2(str string) string {
39+
return str + " somenthing"
40+
}
41+
42+
// 2 param and 2 return
43+
func foo3(str string, str2 string) (string, error) {
44+
if str == "" || str2 == "" {
45+
return "", errors.New("something went wrong")
46+
}
47+
return str + str2, nil
48+
}
49+
50+
func dogYears(name string, age int) (string, int) {
51+
return fmt.Sprint(name, " is this old in dog years "), age * 7
52+
}
53+
54+
// variadic function
55+
func sum(ii ...int) int {
56+
fmt.Println(ii)
57+
fmt.Printf("%T\n", ii)
58+
59+
n := 0
60+
for _, value := range ii {
61+
n += value
62+
}
63+
return n
64+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"strconv"
7+
)
8+
9+
// basic struct type
10+
type book struct {
11+
title string
12+
}
13+
14+
// method implemetation
15+
func (b book) String() string {
16+
return fmt.Sprint("Book title: ", b.title)
17+
}
18+
19+
// basic type
20+
type count int
21+
22+
// method implementation
23+
func (c count) String() string {
24+
return fmt.Sprint("The number is ", strconv.Itoa(int(c)))
25+
}
26+
27+
func main() {
28+
b := book{
29+
title: "Learning to learn",
30+
}
31+
32+
var c count = 42
33+
34+
logInfo(b)
35+
logInfo(c)
36+
}
37+
38+
func logInfo(s fmt.Stringer) {
39+
log.Println("Log from section19:", s.String())
40+
}

src/udemy/go_ted/section19/methods.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package main
2+
3+
import "fmt"
4+
5+
// simple type example
6+
type person struct {
7+
first string
8+
}
9+
10+
type secretAgent struct {
11+
person
12+
ltk bool
13+
}
14+
15+
// method implementation
16+
func (p person) speak() {
17+
fmt.Println("My name is", p.first)
18+
}
19+
20+
func (sa secretAgent) speak() {
21+
fmt.Println("Im a secret agent", sa.first)
22+
}
23+
24+
// interface help us to have polymorphism
25+
// interfaces in Go define a set of method signatures.
26+
27+
type human interface {
28+
speak()
29+
}
30+
31+
// function to receive a parameter of human interface and "speak"
32+
// when a type implements the called method in interfaces, it are of this interfaces type
33+
// like secreAgent and person
34+
35+
func saySomething(h human) {
36+
h.speak()
37+
}
38+
39+
func main() {
40+
sa1 := secretAgent{
41+
person: person{
42+
first: "James",
43+
},
44+
ltk: true,
45+
}
46+
47+
p2 := person{
48+
first: "Janny",
49+
}
50+
51+
saySomething(sa1)
52+
saySomething(p2)
53+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package main
2+
3+
func main() {
4+
println(factorial(4))
5+
}
6+
7+
func factorial(n int) int {
8+
println("this is n", n)
9+
if n == 0 {
10+
return 1
11+
}
12+
return factorial(n-1) * n
13+
}

0 commit comments

Comments
 (0)