Skip to content

Commit 385de67

Browse files
feat: initial commit
0 parents  commit 385de67

File tree

17 files changed

+409
-0
lines changed

17 files changed

+409
-0
lines changed

Dockerfile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
FROM golang:latest
2+
3+
WORKDIR /usr/src/app
4+
5+
COPY go.mod go.sum ./
6+
RUN go mod download && go mod verify
7+
8+
COPY . .
9+
10+
CMD [ "tail", "-f", "/dev/null" ]

Dockerfile.prod

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM golang:latest
2+
3+
WORKDIR /usr/src/app
4+
5+
COPY go.mod go.sum ./
6+
RUN go mod download && go mod verify
7+
8+
COPY . .
9+
RUN go build -v -o /usr/local/bin/app
10+
11+
CMD ["app"]

docker-compose.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
services:
2+
go-app:
3+
build: .
4+
container_name: go-app
5+
volumes:
6+
- ./:/usr/src/app

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module herculesgabriel/golang-playground
2+
3+
go 1.20
4+
5+
require github.com/google/uuid v1.3.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
2+
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=

main.go

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"time"
7+
8+
"herculesgabriel/golang-playground/src/jobs"
9+
"herculesgabriel/golang-playground/src/schemas"
10+
mail "herculesgabriel/golang-playground/src/services/mail/implementations"
11+
"herculesgabriel/golang-playground/src/usecases"
12+
"herculesgabriel/golang-playground/src/utils"
13+
)
14+
15+
func basicLearnings() {
16+
divisionResult, err := utils.Divide(0, 2)
17+
if err != nil {
18+
utils.Logger("Error while dividing values")
19+
}
20+
21+
sumResult := utils.Sum(1, 2)
22+
sumAllResult := utils.SumAll(1, 2, 3)
23+
24+
fmt.Println("0 / 2 =", divisionResult)
25+
fmt.Println("1 + 2 =", sumResult)
26+
fmt.Println("1 + 2 + 3 =", sumAllResult)
27+
}
28+
29+
func threads() {
30+
go jobs.Countdown("second countdown", 5)
31+
jobs.Countdown("first countdown", 10)
32+
}
33+
34+
func channels() {
35+
stringChannel := make(chan string)
36+
37+
go func() {
38+
stringChannel <- "Hello,"
39+
stringChannel <- "World!"
40+
stringChannel <- "-"
41+
}()
42+
43+
var endSignal string
44+
for endSignal != "-" {
45+
msg := <-stringChannel
46+
47+
if msg == "-" {
48+
endSignal = msg
49+
continue
50+
}
51+
52+
time.Sleep(time.Second)
53+
fmt.Println(msg)
54+
}
55+
}
56+
57+
func arraysAndSlices() {
58+
var array [2]int = [2]int{1, 2}
59+
fmt.Println(array)
60+
61+
var slice []int = []int{1, 2}
62+
fmt.Println(slice)
63+
64+
var newSlice []int = make([]int, 2)
65+
fmt.Println(newSlice)
66+
}
67+
68+
func maps() {
69+
var activeModules map[string]bool = make(map[string]bool)
70+
activeModules["finances"] = true
71+
activeModules["health"] = false
72+
73+
emails := map[string]string{
74+
75+
"kate": "[email protected]",
76+
}
77+
78+
fmt.Println(activeModules)
79+
fmt.Println(emails["bob"])
80+
81+
delete(emails, "kate")
82+
_, exists := activeModules["kate"]
83+
fmt.Println(exists)
84+
}
85+
86+
func structs() {
87+
someone := schemas.Person{
88+
Name: "Someone",
89+
Age: 27,
90+
Address: schemas.Address{
91+
City: "Curitiba",
92+
Country: "Brasil",
93+
},
94+
}
95+
96+
city := schemas.Address{
97+
City: "Colombo",
98+
Country: "Brasil",
99+
}
100+
101+
someoneElse := schemas.Person{
102+
Name: "Someone Else",
103+
Age: 26,
104+
Address: city,
105+
}
106+
107+
supermarket := schemas.Place{
108+
Name: "Menudo",
109+
Code: 1,
110+
Address: city,
111+
}
112+
113+
fmt.Println(someone)
114+
fmt.Println(someoneElse)
115+
fmt.Println(supermarket)
116+
117+
fmt.Println(supermarket.Address.City)
118+
fmt.Println(supermarket.City)
119+
120+
fmt.Println(someone.Greeting())
121+
}
122+
123+
func serializer() {
124+
team := schemas.NewTeam()
125+
126+
team.AddPlayer("Bob")
127+
team.AddPlayer("Kate")
128+
129+
result, _ := json.Marshal(team)
130+
131+
data := []byte(result)
132+
var parsedTeam schemas.Team
133+
json.Unmarshal(data, &parsedTeam)
134+
135+
fmt.Println("JSON -> " + string(result))
136+
fmt.Println()
137+
fmt.Print("Team -> ")
138+
fmt.Println(parsedTeam)
139+
}
140+
141+
func interfaces() {
142+
goodMail := mail.GoodMail{}
143+
sendPromoEmail := usecases.NewSendPromoEmail(goodMail)
144+
145+
sendPromoEmail.Execute("[email protected]")
146+
}
147+
148+
func Runner(funcs ...func()) {
149+
for _, fn := range funcs {
150+
fn()
151+
fmt.Println()
152+
}
153+
}
154+
155+
func main() {
156+
funcs := []func(){
157+
basicLearnings,
158+
threads,
159+
channels,
160+
arraysAndSlices,
161+
maps,
162+
structs,
163+
serializer,
164+
interfaces,
165+
}
166+
167+
Runner(funcs...)
168+
169+
170+
171+
}

src/jobs/countdown.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package jobs
2+
3+
import (
4+
"fmt"
5+
"time"
6+
)
7+
8+
func Countdown(title string, seconds int) {
9+
for ; seconds > 0; seconds-- {
10+
fmt.Println(title, ":", seconds)
11+
time.Sleep(time.Second)
12+
}
13+
fmt.Println(title, ":", seconds)
14+
}

src/schemas/address.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package schemas
2+
3+
type Address struct {
4+
City string
5+
Country string
6+
}

src/schemas/person.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package schemas
2+
3+
type Person struct {
4+
Name string
5+
Age int
6+
Address
7+
}
8+
9+
func (p Person) Greeting() string {
10+
return "Hello" + " " + p.Name
11+
}

src/schemas/place.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package schemas
2+
3+
type Place struct {
4+
Name string
5+
Code int
6+
Address
7+
}

0 commit comments

Comments
 (0)