Skip to content

Commit bdabb1d

Browse files
committed
workshop
Signed-off-by: divinerapier <[email protected]>
0 parents  commit bdabb1d

9 files changed

+1414
-0
lines changed

Dockerfile

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Start from the latest golang base image
2+
FROM golang:latest
3+
4+
# Add Maintainer Info
5+
LABEL maintainer="[email protected]"
6+
7+
# Set the Current Working Directory inside the container
8+
WORKDIR /app
9+
10+
# Copy go mod and sum files
11+
COPY go.mod go.sum ./
12+
13+
# Download all dependencies. Dependencies will be cached if the go.mod and go.sum files are not changed
14+
RUN go mod download
15+
16+
# Copy the source from the current directory to the Working Directory inside the container
17+
COPY . .
18+
19+
# Build the Go app
20+
RUN go build -o main .
21+
22+
# Expose port 8080 to the outside world
23+
EXPOSE 8080
24+
25+
# Command to run the executable
26+
CMD ["./main"]

beer.go

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package main
2+
3+
import (
4+
"sort"
5+
"sync"
6+
)
7+
8+
type Beers struct {
9+
beers []Beer
10+
mu sync.Mutex
11+
}
12+
13+
// sort beers by id
14+
func (beers *Beers) Len() int {
15+
beers.mu.Lock()
16+
defer beers.mu.Unlock()
17+
return len(beers.beers)
18+
}
19+
func (beer *Beers) Less(i, j int) bool {
20+
beer.mu.Lock()
21+
defer beer.mu.Unlock()
22+
return beer.beers[i].ID < beer.beers[j].ID
23+
}
24+
func (beer *Beers) Swap(i, j int) {
25+
beer.mu.Lock()
26+
defer beer.mu.Unlock()
27+
beer.beers[i], beer.beers[j] = beer.beers[j], beer.beers[i]
28+
}
29+
30+
func NewBeers(beers []Beer) *Beers {
31+
return &Beers{
32+
beers: beers,
33+
}
34+
}
35+
36+
func (bs *Beers) GetRemains() []Beer {
37+
bs.mu.Lock()
38+
defer bs.mu.Unlock()
39+
sort.Sort(bs)
40+
return bs.beers
41+
}
42+
43+
func (bs *Beers) GetByID(id int) (*Beer, bool) {
44+
bs.mu.Lock()
45+
defer bs.mu.Unlock()
46+
for _, beer := range bs.beers {
47+
if beer.ID == id {
48+
return &beer, true
49+
}
50+
}
51+
return nil, false
52+
}

0 commit comments

Comments
 (0)