Start translation to brazilian portuguese
This commit is contained in:
26
pt-br/code/src/apps/ch.2.3/basic_functions/main.go
Normal file
26
pt-br/code/src/apps/ch.2.3/basic_functions/main.go
Normal file
@@ -0,0 +1,26 @@
|
||||
// Example code for Chapter 2.3 from "Build Web Application with Golang"
|
||||
// Purpose: Creating a basic function
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
// return greater value between a and b
|
||||
func max(a, b int) int {
|
||||
if a > b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func main() {
|
||||
x := 3
|
||||
y := 4
|
||||
z := 5
|
||||
|
||||
max_xy := max(x, y) // call function max(x, y)
|
||||
max_xz := max(x, z) // call function max(x, z)
|
||||
|
||||
fmt.Printf("max(%d, %d) = %d\n", x, y, max_xy)
|
||||
fmt.Printf("max(%d, %d) = %d\n", x, z, max_xz)
|
||||
fmt.Printf("max(%d, %d) = %d\n", y, z, max(y, z)) // call function here
|
||||
}
|
||||
14
pt-br/code/src/apps/ch.2.3/hidden_print_methods/main.go
Normal file
14
pt-br/code/src/apps/ch.2.3/hidden_print_methods/main.go
Normal file
@@ -0,0 +1,14 @@
|
||||
// As of Google go 1.1.2, `println()` and `print()` are hidden functions included from the runtime package.
|
||||
// However it's encouraged to use the print functions from the `fmt` package.
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func f() {
|
||||
fmt.Println("First")
|
||||
print("Second ")
|
||||
println(" Third")
|
||||
}
|
||||
func main() {
|
||||
f()
|
||||
}
|
||||
26
pt-br/code/src/apps/ch.2.3/import_packages/main.go
Normal file
26
pt-br/code/src/apps/ch.2.3/import_packages/main.go
Normal file
@@ -0,0 +1,26 @@
|
||||
// Example code for Chapter 2.3 from "Build Web Application with Golang"
|
||||
// Purpose: Shows different ways of importing a package.
|
||||
// Note: For the package `only_call_init`, we reference the path from the
|
||||
// base directory of `$GOPATH/src`. The reason being Golang discourage
|
||||
// the use of relative paths when import packages.
|
||||
// BAD: "./only_call_init"
|
||||
// GOOD: "apps/ch.2.3/import_packages/only_call_init"
|
||||
package main
|
||||
|
||||
import (
|
||||
// `_` will only call init() inside the package only_call_init
|
||||
_ "apps/ch.2.3/import_packages/only_call_init"
|
||||
f "fmt" // import the package as `f`
|
||||
. "math" // makes the public methods and constants global
|
||||
"mymath" // custom package located at $GOPATH/src/
|
||||
"os" // normal import of a standard package
|
||||
"text/template" // the package takes the name of last folder path, `template`
|
||||
)
|
||||
|
||||
func main() {
|
||||
f.Println("mymath.Sqrt(4) =", mymath.Sqrt(4))
|
||||
f.Println("E =", E) // references math.E
|
||||
|
||||
t, _ := template.New("test").Parse("Pi^2 = {{.}}")
|
||||
t.Execute(os.Stdout, Pow(Pi, 2))
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package only_call_init
|
||||
|
||||
import "fmt"
|
||||
|
||||
func init() {
|
||||
fmt.Println("only_call_init.init() was called.")
|
||||
}
|
||||
142
pt-br/code/src/apps/ch.2.3/main.go
Normal file
142
pt-br/code/src/apps/ch.2.3/main.go
Normal file
@@ -0,0 +1,142 @@
|
||||
// Example code for Chapter 2.3 from "Build Web Application with Golang"
|
||||
// Purpose: Goes over if, else, switch conditions, loops and defer.
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func computedValue() int {
|
||||
return 1
|
||||
}
|
||||
func show_if() {
|
||||
fmt.Println("\n#show_if()")
|
||||
x := computedValue()
|
||||
integer := 23
|
||||
|
||||
fmt.Println("x =", x)
|
||||
fmt.Println("integer =", integer)
|
||||
if x > 10 {
|
||||
fmt.Println("x is greater than 10")
|
||||
} else {
|
||||
fmt.Println("x is less than 10")
|
||||
}
|
||||
|
||||
if integer == 3 {
|
||||
fmt.Println("The integer is equal to 3")
|
||||
} else if integer < 3 {
|
||||
fmt.Println("The integer is less than 3")
|
||||
} else {
|
||||
fmt.Println("The integer is greater than 3")
|
||||
}
|
||||
}
|
||||
func show_if_var() {
|
||||
fmt.Println("\n#show_if_var()")
|
||||
// initialize x, then check if x greater than
|
||||
if x := computedValue(); x > 10 {
|
||||
fmt.Println("x is greater than 10")
|
||||
} else {
|
||||
fmt.Println("x is less than 10")
|
||||
}
|
||||
|
||||
// the following code will not compile, since `x` is only accessable with the if/else block
|
||||
// fmt.Println(x)
|
||||
}
|
||||
func show_goto() {
|
||||
fmt.Println("\n#show_goto()")
|
||||
// The call to the label switches the goroutine it seems.
|
||||
i := 0
|
||||
Here: // label ends with ":"
|
||||
fmt.Println(i)
|
||||
i++
|
||||
if i < 10 {
|
||||
goto Here // jump to label "Here"
|
||||
}
|
||||
}
|
||||
func show_for_loop() {
|
||||
fmt.Println("\n#show_for_loop()")
|
||||
sum := 0
|
||||
for index := 0; index < 10; index++ {
|
||||
sum += index
|
||||
}
|
||||
fmt.Println("part 1, sum is equal to ", sum)
|
||||
|
||||
sum = 1
|
||||
// The compiler will remove the `;` from the line below.
|
||||
// for ; sum < 1000 ; {
|
||||
for sum < 1000 {
|
||||
sum += sum
|
||||
}
|
||||
fmt.Println("part 2, sum is equal to ", sum)
|
||||
|
||||
for index := 10; 0 < index; index-- {
|
||||
if index == 5 {
|
||||
break // or continue
|
||||
}
|
||||
fmt.Println(index)
|
||||
}
|
||||
|
||||
}
|
||||
func show_loop_through_map() {
|
||||
fmt.Println("\n#show_loop_through_map()")
|
||||
m := map[string]int{
|
||||
"one": 1,
|
||||
"two": 2,
|
||||
"three": 3,
|
||||
}
|
||||
fmt.Println("map value = ", m)
|
||||
for k, v := range m {
|
||||
fmt.Println("map's key: ", k)
|
||||
fmt.Println("map's value: ", v)
|
||||
}
|
||||
}
|
||||
func show_switch() {
|
||||
fmt.Println("\n#show_switch()")
|
||||
i := 10
|
||||
switch i {
|
||||
case 1:
|
||||
fmt.Println("i is equal to 1")
|
||||
case 2, 3, 4:
|
||||
fmt.Println("i is equal to 2, 3 or 4")
|
||||
case 10:
|
||||
fmt.Println("i is equal to 10")
|
||||
default:
|
||||
fmt.Println("All I know is that i is an integer")
|
||||
}
|
||||
|
||||
integer := 6
|
||||
fmt.Println("integer =", integer)
|
||||
switch integer {
|
||||
case 4:
|
||||
fmt.Println("integer == 4")
|
||||
fallthrough
|
||||
case 5:
|
||||
fmt.Println("integer <= 5")
|
||||
fallthrough
|
||||
case 6:
|
||||
fmt.Println("integer <= 6")
|
||||
fallthrough
|
||||
case 7:
|
||||
fmt.Println("integer <= 7")
|
||||
fallthrough
|
||||
case 8:
|
||||
fmt.Println("integer <= 8")
|
||||
fallthrough
|
||||
default:
|
||||
fmt.Println("default case")
|
||||
}
|
||||
}
|
||||
func show_defer() {
|
||||
fmt.Println("\nshow_defer()")
|
||||
defer fmt.Println("(last defer)")
|
||||
for i := 0; i < 5; i++ {
|
||||
defer fmt.Printf("%d ", i)
|
||||
}
|
||||
}
|
||||
func main() {
|
||||
show_if()
|
||||
show_if_var()
|
||||
show_goto()
|
||||
show_for_loop()
|
||||
show_loop_through_map()
|
||||
show_switch()
|
||||
show_defer()
|
||||
}
|
||||
31
pt-br/code/src/apps/ch.2.3/panic_and_recover/main.go
Normal file
31
pt-br/code/src/apps/ch.2.3/panic_and_recover/main.go
Normal file
@@ -0,0 +1,31 @@
|
||||
// Example code for Chapter 2.3 from "Build Web Application with Golang"
|
||||
// Purpose: Showing how to use `panic()` and `recover()`
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
var user = os.Getenv("USER")
|
||||
|
||||
func check_user() {
|
||||
if user == "" {
|
||||
panic("no value for $USER")
|
||||
}
|
||||
fmt.Println("Environment Variable `USER` =", user)
|
||||
}
|
||||
func throwsPanic(f func()) (b bool) {
|
||||
defer func() {
|
||||
if x := recover(); x != nil {
|
||||
fmt.Println("Panic message =", x);
|
||||
b = true
|
||||
}
|
||||
}()
|
||||
f() // if f causes panic, it will recover
|
||||
return
|
||||
}
|
||||
func main(){
|
||||
didPanic := throwsPanic(check_user)
|
||||
fmt.Println("didPanic =", didPanic)
|
||||
}
|
||||
31
pt-br/code/src/apps/ch.2.3/pass_by_value_and_pointer/main.go
Normal file
31
pt-br/code/src/apps/ch.2.3/pass_by_value_and_pointer/main.go
Normal file
@@ -0,0 +1,31 @@
|
||||
// Example code for Chapter 2.3 from "Build Web Application with Golang"
|
||||
// Purpose: Shows passing a variable by value and reference
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func add_by_value(a int) int {
|
||||
a = a + 1
|
||||
return a
|
||||
}
|
||||
func add_by_reference(a *int) int {
|
||||
*a = *a + 1
|
||||
return *a
|
||||
}
|
||||
func show_add_by_value() {
|
||||
x := 3
|
||||
fmt.Println("x = ", x)
|
||||
fmt.Println("add_by_value(x) =", add_by_value(x) )
|
||||
fmt.Println("x = ", x)
|
||||
}
|
||||
func show_add_by_reference() {
|
||||
x := 3
|
||||
fmt.Println("x = ", x)
|
||||
// &x pass memory address of x
|
||||
fmt.Println("add_by_reference(&x) =", add_by_reference(&x) )
|
||||
fmt.Println("x = ", x)
|
||||
}
|
||||
func main() {
|
||||
show_add_by_value()
|
||||
show_add_by_reference()
|
||||
}
|
||||
44
pt-br/code/src/apps/ch.2.3/type_function/main.go
Normal file
44
pt-br/code/src/apps/ch.2.3/type_function/main.go
Normal file
@@ -0,0 +1,44 @@
|
||||
// Example code for Chapter 2.3 from "Build Web Application with Golang"
|
||||
// Purpose: Shows how to define a function type
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type testInt func(int) bool // define a function type of variable
|
||||
|
||||
func isOdd(integer int) bool {
|
||||
if integer%2 == 0 {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func isEven(integer int) bool {
|
||||
if integer%2 == 0 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// pass the function `f` as an argument to another function
|
||||
|
||||
func filter(slice []int, f testInt) []int {
|
||||
var result []int
|
||||
for _, value := range slice {
|
||||
if f(value) {
|
||||
result = append(result, value)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
func init() {
|
||||
fmt.Println("\n#init() was called.")
|
||||
}
|
||||
func main() {
|
||||
slice := []int{1, 2, 3, 4, 5, 7}
|
||||
fmt.Println("slice = ", slice)
|
||||
odd := filter(slice, isOdd) // use function as values
|
||||
fmt.Println("Odd elements of slice are: ", odd)
|
||||
even := filter(slice, isEven)
|
||||
fmt.Println("Even elements of slice are: ", even)
|
||||
}
|
||||
20
pt-br/code/src/apps/ch.2.3/variadic_functions/main.go
Normal file
20
pt-br/code/src/apps/ch.2.3/variadic_functions/main.go
Normal file
@@ -0,0 +1,20 @@
|
||||
// Example code for Chapter 2.3 from "Build Web Application with Golang"
|
||||
// Purpose: Shows how to return multiple values from a function
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
// return results of A + B and A * B
|
||||
func SumAndProduct(A, B int) (int, int) {
|
||||
return A + B, A * B
|
||||
}
|
||||
|
||||
func main() {
|
||||
x := 3
|
||||
y := 4
|
||||
|
||||
xPLUSy, xTIMESy := SumAndProduct(x, y)
|
||||
|
||||
fmt.Printf("%d + %d = %d\n", x, y, xPLUSy)
|
||||
fmt.Printf("%d * %d = %d\n", x, y, xTIMESy)
|
||||
}
|
||||
Reference in New Issue
Block a user