-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from 0bvim/feat/section19
feat/section19
- Loading branch information
Showing
17 changed files
with
535 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
func main() { | ||
// not anonymous | ||
foo() | ||
|
||
// anonymous | ||
func() { | ||
fmt.Println("anonymous function") | ||
}() | ||
|
||
// anonymous function with param | ||
func(s string) { | ||
fmt.Println("Anon func showing my name", s) | ||
}("Nivi") | ||
|
||
// can assign it to a variable too | ||
x := func(s string) { | ||
fmt.Println("Anon func showing my name to a func assigned to a variable", s) | ||
} | ||
x("Thompson") | ||
} | ||
|
||
func foo() { | ||
fmt.Println("foo") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"strconv" | ||
) | ||
|
||
// link of example https://www.alexedwards.net/blog/interfaces-explained | ||
|
||
// this is my implementation of a method that satisfy | ||
// String() string (fmt.Stringer()) interface | ||
type Item struct { | ||
Type string | ||
Name string | ||
} | ||
|
||
func (it Item) String() string { | ||
return fmt.Sprintf("Type: %s - Name: %s", it.Type, it.Name) | ||
} | ||
|
||
// Declare a Book type which satisfies the fmt.Stringer interface. | ||
type Book struct { | ||
Title string | ||
Author string | ||
} | ||
|
||
func (b Book) String() string { | ||
return fmt.Sprintf("Book: %s - %s", b.Title, b.Author) | ||
} | ||
|
||
// Declare a Count type which satisfies the fmt.Stringer interface. | ||
type Count int | ||
|
||
func (c Count) String() string { | ||
return strconv.Itoa(int(c)) | ||
} | ||
|
||
// Declare a writeLog function which takes any object that satisfies | ||
// the fmt.Stringer interface. | ||
func WriteLog(s fmt.Stringer) { | ||
log.Println(s.String()) | ||
} | ||
|
||
func main() { | ||
// Initialize a Count object and pass it to WriteLog(). | ||
book := Book{"Alice in Wonderland", "Lewis Carrol"} | ||
WriteLog(book) | ||
|
||
// Initialize a Count object and pass it to WriteLog(). | ||
count := Count(3) | ||
WriteLog(count) | ||
|
||
item := Item{ | ||
Type: "Eletronic", | ||
Name: "Notebook", | ||
} | ||
WriteLog(item) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package main | ||
|
||
func main() { | ||
println(doMath(41, 1, add)) | ||
println(doMath(43, 1, subtract)) | ||
println(doMath(21, 2, multiply)) | ||
println(doMath(84, 2, division)) | ||
} | ||
|
||
// callback function are like function that receive a function pointer as param in 'c' | ||
func doMath(a, b int, f func(int, int) int) int { | ||
return f(a, b) | ||
} | ||
|
||
func add(a, b int) int { | ||
return a + b | ||
} | ||
|
||
func subtract(a, b int) int { | ||
return a - b | ||
} | ||
|
||
func division(a, b int) int { | ||
return a / b | ||
} | ||
|
||
func multiply(a, b int) int { | ||
return a * b | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
func main() { | ||
f := incrementor() | ||
fmt.Printf("value: %v\tMem: %v\n", f(), &f) | ||
fmt.Printf("value: %v\tMem: %v\n", f(), &f) | ||
fmt.Printf("value: %v\tMem: %v\n", f(), &f) | ||
|
||
println("--------------") | ||
|
||
// here, like in 'f' var above the function assigned to the 'g' variable will | ||
// be in the same memory place. | ||
// so when you call, it will have the same value stored into deeper variable 'x' | ||
g := incrementor() | ||
fmt.Printf("value: %v\tMem: %v\n", g(), &g) | ||
fmt.Printf("value: %v\tMem: %v\n", g(), &g) | ||
fmt.Printf("value: %v\tMem: %v\n", g(), &g) | ||
|
||
println("--------------") | ||
|
||
// if you don't assign to a variable it will return initial state of variable 'x' | ||
// because it's stored in a different place in memory | ||
fmt.Printf("Mem: %v\n", incrementor()) | ||
fmt.Printf("Mem: %v\n", incrementor()) | ||
fmt.Printf("Mem: %v\n", incrementor()) | ||
} | ||
|
||
func incrementor() func() int { | ||
x := 0 | ||
return func() int { | ||
x++ | ||
return x | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
func main() { | ||
// when we use defer (like 'adiar' in pt_br) it's like delay when the action of function will happens | ||
{ | ||
foo() | ||
bar() | ||
} | ||
// in this case foo will be printed after bar | ||
{ | ||
defer foo() | ||
bar() | ||
} | ||
} | ||
|
||
// func prototype | ||
// func (r receiver) identifier(p parameter(s)) (r return(s)) { <code? } | ||
|
||
func foo() { | ||
fmt.Println("foo") | ||
} | ||
|
||
func bar() { | ||
fmt.Println("bar") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package main | ||
|
||
import . "fmt" | ||
|
||
func main() { | ||
x := foo() | ||
Println(x) | ||
|
||
y := bar() | ||
Println(y()) | ||
Printf("%T\n", foo()) | ||
Printf("%T\n", bar()) | ||
Printf("%T\n", y) | ||
} | ||
|
||
func foo() int { | ||
return 42 | ||
} | ||
|
||
func bar() func() int { | ||
return func() int { | ||
return 43 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,64 @@ | ||
package main | ||
|
||
import "fmt" | ||
import ( | ||
"errors" | ||
"fmt" | ||
"log" | ||
) | ||
|
||
func main() { | ||
foo() | ||
foo1("xpto") | ||
fmt.Println(foo2("I want")) | ||
rtn, err := foo3("I want", " more") | ||
if err != nil { | ||
log.Fatalf("error while calling foo3: %v", err) | ||
} | ||
fmt.Println(rtn) | ||
fmt.Println(dogYears("Vinicius", 31)) | ||
fmt.Println(sum(1, 2, 3, 4, 5, 6, 7)) | ||
|
||
// unfurling a slice | ||
x := []int{1, 2, 3, 4, 5} | ||
fmt.Println(sum(x...)) | ||
|
||
} | ||
|
||
// no params and no return | ||
func foo() { | ||
fmt.Println("I am from foo") | ||
} | ||
|
||
// no return | ||
// 1 param no return | ||
func foo1(str string) { | ||
fmt.Println("I am from foo", str) | ||
} | ||
|
||
// 1 param and 1 return | ||
func foo2(str string) string { | ||
return str + " somenthing" | ||
} | ||
|
||
// 2 param and 2 return | ||
func foo3(str string, str2 string) (string, error) { | ||
if str == "" || str2 == "" { | ||
return "", errors.New("something went wrong") | ||
} | ||
return str + str2, nil | ||
} | ||
|
||
func dogYears(name string, age int) (string, int) { | ||
return fmt.Sprint(name, " is this old in dog years "), age * 7 | ||
} | ||
|
||
// variadic function | ||
func sum(ii ...int) int { | ||
fmt.Println(ii) | ||
fmt.Printf("%T\n", ii) | ||
|
||
n := 0 | ||
for _, value := range ii { | ||
n += value | ||
} | ||
return n | ||
} |
40 changes: 40 additions & 0 deletions
40
src/udemy/go_ted/section19/interfaces_polymorph-stringer.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"strconv" | ||
) | ||
|
||
// basic struct type | ||
type book struct { | ||
title string | ||
} | ||
|
||
// method implemetation | ||
func (b book) String() string { | ||
return fmt.Sprint("Book title: ", b.title) | ||
} | ||
|
||
// basic type | ||
type count int | ||
|
||
// method implementation | ||
func (c count) String() string { | ||
return fmt.Sprint("The number is ", strconv.Itoa(int(c))) | ||
} | ||
|
||
func main() { | ||
b := book{ | ||
title: "Learning to learn", | ||
} | ||
|
||
var c count = 42 | ||
|
||
logInfo(b) | ||
logInfo(c) | ||
} | ||
|
||
func logInfo(s fmt.Stringer) { | ||
log.Println("Log from section19:", s.String()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
// simple type example | ||
type person struct { | ||
first string | ||
} | ||
|
||
type secretAgent struct { | ||
person | ||
ltk bool | ||
} | ||
|
||
// method implementation | ||
func (p person) speak() { | ||
fmt.Println("My name is", p.first) | ||
} | ||
|
||
func (sa secretAgent) speak() { | ||
fmt.Println("Im a secret agent", sa.first) | ||
} | ||
|
||
// interface help us to have polymorphism | ||
// interfaces in Go define a set of method signatures. | ||
|
||
type human interface { | ||
speak() | ||
} | ||
|
||
// function to receive a parameter of human interface and "speak" | ||
// when a type implements the called method in interfaces, it are of this interfaces type | ||
// like secreAgent and person | ||
|
||
func saySomething(h human) { | ||
h.speak() | ||
} | ||
|
||
func main() { | ||
sa1 := secretAgent{ | ||
person: person{ | ||
first: "James", | ||
}, | ||
ltk: true, | ||
} | ||
|
||
p2 := person{ | ||
first: "Janny", | ||
} | ||
|
||
saySomething(sa1) | ||
saySomething(p2) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package main | ||
|
||
func main() { | ||
println(factorial(4)) | ||
} | ||
|
||
func factorial(n int) int { | ||
println("this is n", n) | ||
if n == 0 { | ||
return 1 | ||
} | ||
return factorial(n-1) * n | ||
} |
Oops, something went wrong.