Skip to content

Commit b4cecd9

Browse files
author
Amador Quispe
committed
#8 - GO
1 parent a76bde9 commit b4cecd9

File tree

1 file changed

+149
-0
lines changed

1 file changed

+149
-0
lines changed
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
package main
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
)
7+
8+
// En GO las clases son diferentes a los demás lenguajes, se usan structs
9+
type Student struct {
10+
id int
11+
name string
12+
}
13+
14+
// Constructor
15+
func NewStudent() Student {
16+
return Student{}
17+
}
18+
19+
// También es posible simular los métodos setter y getter
20+
func (s *Student) SetId(id int) {
21+
s.id = id
22+
}
23+
func (s *Student) SetName(name string) {
24+
s.name = name
25+
}
26+
27+
func (s *Student) GetId() int {
28+
return s.id
29+
}
30+
31+
func (s *Student) GetName() string {
32+
return s.name
33+
}
34+
35+
func (s *Student) ToString() string {
36+
return fmt.Sprintf("{Id: %d, Name: %s}", s.id, s.name)
37+
}
38+
39+
// Pila LIFO último en entrar, primero en salir
40+
type Stack[T any] struct {
41+
elements []T
42+
}
43+
44+
func NewStack[T any]() Stack[T] {
45+
return Stack[T]{}
46+
}
47+
48+
func (s *Stack[T]) Empty() bool {
49+
return len(s.elements) == 0
50+
}
51+
52+
func (s *Stack[T]) Push(item T) T {
53+
s.elements = append(s.elements, item)
54+
return item
55+
}
56+
57+
func (s *Stack[T]) Pop() (T, error) {
58+
var zero T
59+
if s.Empty() {
60+
return zero, errors.New("stack is empty")
61+
}
62+
item := s.elements[len(s.elements)-1]
63+
s.elements = s.elements[:len(s.elements)-1]
64+
return item, nil
65+
}
66+
67+
func (s *Stack[T]) Peek() (T, error) {
68+
var zero T
69+
if s.Empty() {
70+
return zero, errors.New("stack is empty")
71+
}
72+
return s.elements[len(s.elements)-1], nil
73+
}
74+
75+
func (s *Stack[T]) Size() int {
76+
return len(s.elements)
77+
}
78+
79+
func (s *Stack[T]) Print() {
80+
fmt.Println(s.elements)
81+
}
82+
83+
// Colas FIFO primero en entrar, primero en salir
84+
type Queue[T any] struct {
85+
elements []T
86+
}
87+
88+
func (q *Queue[T]) Empty() bool {
89+
return len(q.elements) == 0
90+
}
91+
func NewQueue[T any]() Queue[T] {
92+
return Queue[T]{}
93+
}
94+
95+
func (q *Queue[T]) Add(item T) T {
96+
q.elements = append(q.elements, item)
97+
return item
98+
}
99+
100+
func (q *Queue[T]) Poll() (T, error) {
101+
var item T
102+
if q.Empty() {
103+
return item, errors.New("queue is empty")
104+
}
105+
item = q.elements[0]
106+
q.elements = q.elements[1:]
107+
return item, nil
108+
}
109+
110+
func (q *Queue[T]) Size() int {
111+
return len(q.elements)
112+
}
113+
114+
func (q *Queue[T]) Print() {
115+
fmt.Println(q.elements)
116+
}
117+
118+
func main() {
119+
//CLASES
120+
student := Student{}
121+
student.SetId(100)
122+
student.SetName("Amador")
123+
fmt.Println(student.ToString())
124+
//PILAS
125+
stack := NewStack[int]()
126+
stack.Push(1)
127+
stack.Push(2)
128+
stack.Push(3)
129+
stack.Push(4)
130+
stack.Push(5)
131+
fmt.Println(stack.Size())
132+
stack.Print()
133+
stack.Pop()
134+
stack.Pop()
135+
fmt.Println(stack.Size())
136+
stack.Print()
137+
138+
//COLAS
139+
queue := NewQueue[string]()
140+
queue.Add("Amador")
141+
queue.Add("Alex")
142+
queue.Add("Pedro")
143+
fmt.Println(queue.Size())
144+
queue.Print()
145+
queue.Poll()
146+
fmt.Println(queue.Size())
147+
queue.Print()
148+
149+
}

0 commit comments

Comments
 (0)