Skip to content

Commit 8f66d6f

Browse files
🔥 🚀
0 parents  commit 8f66d6f

File tree

7 files changed

+429
-0
lines changed

7 files changed

+429
-0
lines changed

collections.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
maps()
7+
}
8+
9+
func slices() {
10+
names := make([]string, 10)
11+
fmt.Println(names)
12+
13+
names[0] = "João"
14+
names[1] = "Pedro"
15+
names[3] = "Miguel"
16+
names[4] = "Maria"
17+
names[5] = "José"
18+
fmt.Println(names)
19+
20+
twoNames := names[0:2]
21+
allNames := names[:len(names)]
22+
23+
fmt.Println(twoNames)
24+
fmt.Println(allNames)
25+
26+
names = append(names, "Fernando")
27+
28+
fmt.Println("New names ", names)
29+
}
30+
31+
func theMatrix() {
32+
var matrix [3][3]int
33+
for i := 0; i < len(matrix); i++ {
34+
for j := 0; j < len(matrix); j++ {
35+
matrix[i][j] = j + i
36+
}
37+
}
38+
fmt.Println("matrix", matrix)
39+
}
40+
41+
func simpleLists() {
42+
var ids [5]int
43+
ages := [10]int{23, 22, 20}
44+
45+
fmt.Println("array:", ids, "\nlength:", len(ids))
46+
fmt.Println("Ages", ages)
47+
}
48+
49+
func playingWithRange() {
50+
51+
var ids = []int{1, 2, 3, 4, 5}
52+
53+
for range ids {
54+
println("I'm inside the range :D")
55+
}
56+
57+
println("------- With Index and Value -------")
58+
59+
for index, value := range ids {
60+
println("Index >>", index, "Value >>", value)
61+
}
62+
}
63+
64+
// A lat, lng struct
65+
type Coordinate struct {
66+
lat, lng float64
67+
}
68+
69+
func maps() {
70+
var places = map[string]Coordinate{
71+
"Bell Labs": Coordinate{40.68433, -74.39967},
72+
"Google": Coordinate{37.42202, -122.08408},
73+
}
74+
75+
places["Another place"] = Coordinate{
76+
lat: 30.68433,
77+
lng: -54.39967,
78+
}
79+
80+
fmt.Println(places)
81+
fmt.Println(places["Bell Labs"])
82+
}

data.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hello, this is a sample text

files.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"os"
7+
"strings"
8+
)
9+
10+
func main() {
11+
readFromFile()
12+
}
13+
14+
func readFromFile() {
15+
// .:.:.: IO Util makes it easy :.:.:.
16+
// fileName := "data.txt"
17+
// bytes, _ := ioutil.ReadFile(fileName)
18+
// fmt.Println(string(bytes))
19+
20+
// .:.:.: Reading to a byte array :.:.:.
21+
file, _ := os.Open("data.txt")
22+
fileInfo, _ := file.Stat()
23+
24+
// With the [FileInfo.Size()] I create a byte array with this file's size
25+
bytesData := make([]byte, fileInfo.Size())
26+
length, _ := file.Read(bytesData)
27+
28+
fmt.Println(string(bytesData[:length]))
29+
}
30+
31+
func reader() {
32+
reader := strings.NewReader("Hello world")
33+
bytes := make([]byte, 3)
34+
35+
for {
36+
length, err := reader.Read(bytes)
37+
fmt.Printf("byte = %s\n", bytes[:length])
38+
if err == io.EOF {
39+
break
40+
}
41+
}
42+
}

hello.go

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"math"
6+
"math/cmplx"
7+
"math/rand"
8+
"runtime"
9+
"time"
10+
)
11+
12+
type number int
13+
14+
func (n number) isPositive() bool {
15+
return n > 0
16+
}
17+
18+
func main() {
19+
20+
// var age number = -42
21+
// fmt.Println(age.isPositive())
22+
23+
// b := make([]int, 2)
24+
// fmt.Println(b)
25+
26+
// sum := add(21, 21)
27+
// fmt.Println("Sum is", sum)
28+
29+
// a, b := swap("world", "hello")
30+
// fmt.Println(a, b)
31+
32+
// x, y := split(2)
33+
// fmt.Println(x, y)
34+
35+
// variables()
36+
37+
// loops()
38+
39+
// ifStatements()
40+
41+
// switchSamples()
42+
43+
// deferSamples()
44+
}
45+
46+
func helloWorld() {
47+
fmt.Printf("Hello World\n")
48+
}
49+
50+
func mathStuff() {
51+
fmt.Println("That's a random number: ", rand.Intn(10))
52+
fmt.Println("PI is: ", math.Pi)
53+
}
54+
55+
func whatTimeIsIt() {
56+
fmt.Println("Current time: ", time.Now())
57+
}
58+
59+
// When you have two consecutive variables with the same type
60+
// into your function you can write the type only in the last variable
61+
func add(x int, y int) int {
62+
return x + y
63+
}
64+
65+
// in Go you can have many returns
66+
func swap(s1, s2 string) (string, string) {
67+
return s2, s1
68+
}
69+
70+
// returns in Go can be named :O
71+
func split(sum int) (x, y int) {
72+
x = sum * 4 / 9
73+
y = sum - x
74+
return
75+
}
76+
77+
func variables() {
78+
// var defines a variable that can be assigned later
79+
var x int
80+
fmt.Println(x) // default value for an int unassigned is 0 :D
81+
82+
x = 2
83+
fmt.Println(x)
84+
x = 1
85+
fmt.Println(x)
86+
x = 0
87+
fmt.Println(x)
88+
89+
// Inside a function, the := short
90+
// assignment statement can be used in
91+
// place of a var declaration with implicit type.
92+
y := 21
93+
y = 42
94+
fmt.Println(y)
95+
96+
// You can create variables inline
97+
var c, python, java = true, false, "no! :D"
98+
fmt.Println(c, python, java)
99+
100+
var (
101+
isAvailable bool = false
102+
z complex128 = cmplx.Sqrt(-5 + 12i)
103+
)
104+
105+
fmt.Printf("Type: %T | Value: %v\n", isAvailable, isAvailable)
106+
fmt.Printf("Type: %T | Value: %v\n", z, z)
107+
108+
const (
109+
Big = 1 << 2
110+
)
111+
a := float32(Big)
112+
fmt.Println(a)
113+
}
114+
115+
func loops() {
116+
// for
117+
sum := 0
118+
for i := 0; i < 10; i++ {
119+
sum += i
120+
}
121+
fmt.Println("Sum is:", sum)
122+
123+
// "while"
124+
for sum < 0 {
125+
sum = sum - 1
126+
}
127+
fmt.Println("Now sum is:", sum)
128+
129+
// Infinity loop
130+
// for { }
131+
}
132+
133+
func ifStatements() {
134+
age := 17
135+
if age >= 18 {
136+
fmt.Println("Yes")
137+
} else {
138+
fmt.Println("No")
139+
}
140+
}
141+
142+
func switchSamples() {
143+
fmt.Print("Go runs on ")
144+
145+
switch os := runtime.GOOS; os {
146+
case "darwin":
147+
fmt.Print("OS X\n")
148+
case "linux":
149+
fmt.Print("Linux\n")
150+
default:
151+
fmt.Print(os, "\n")
152+
}
153+
154+
fmt.Println("\nWhen's Saturday?")
155+
today := time.Now().Weekday()
156+
switch time.Saturday {
157+
case today + 0:
158+
fmt.Println("Today.")
159+
case today + 1:
160+
fmt.Println("Tomorrow.")
161+
case today + 2:
162+
fmt.Println("In two days.")
163+
default:
164+
fmt.Println("Too far away.")
165+
}
166+
167+
// Switch with conditions
168+
hour := time.Now().Hour()
169+
switch {
170+
case hour < 12:
171+
fmt.Println("Good morning!")
172+
case hour < 17:
173+
fmt.Println("Good afternoon.")
174+
default:
175+
fmt.Println("Good evening.")
176+
}
177+
}
178+
179+
func deferSamples() {
180+
fmt.Println("counting...")
181+
182+
i := 0
183+
for i < 10 {
184+
defer fmt.Println(i)
185+
i++
186+
}
187+
188+
fmt.Println("done")
189+
}

interfaces.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package main
2+
3+
import "fmt"
4+
5+
// I interface is a simple contract
6+
type I interface {
7+
M()
8+
}
9+
10+
// T is a struct which has a property S which is a string
11+
type T struct {
12+
S string
13+
}
14+
15+
// M is the implementation of I.M() but
16+
// implementing inside T
17+
func (t T) M() {
18+
fmt.Println(t.S)
19+
}
20+
21+
func main() {
22+
// var i I = T{"hello! :)"}
23+
// i.M()
24+
25+
theEmptyInterface()
26+
}
27+
28+
func theEmptyInterface() {
29+
var i interface{}
30+
describe(i)
31+
32+
i = 42
33+
describe(i)
34+
35+
i = "hello"
36+
describe(i)
37+
}
38+
39+
func describe(i interface{}) {
40+
fmt.Printf("(%v, %T)\n", i, i)
41+
}

0 commit comments

Comments
 (0)