Accelerating API Development: A Pit Stop with Gin-Gonic in Golang-Slide.pdf
1. 26/01/24, 10:35 Gof1 Talk
https://xm1k3.github.io/gof1-talk-ita/ 1/41
ACCELERATING API DEVELOPMENT:
A Pit Stop with Gin-Gonic 🥃in Golang
2. 26/01/24, 10:35 Gof1 Talk
https://xm1k3.github.io/gof1-talk-ita/ 2/41
PANORAMICA DEL TALK
Introduzione
Panoramica di GoLang
Introduzione a Gin
Confronto tra Gin/Echo/Fiber
Architettura del Progetto gof1
Creazione di API RESTful con Gin
Tests
Q&A
3. 26/01/24, 10:35 Gof1 Talk
https://xm1k3.github.io/gof1-talk-ita/ 3/41
CHI SONO
Backend Developer - Cybersecurity Enthusiast
Sviluppo principalmente in Golang. Nel tempo libero mi interesso di Crypto e
Cybersecurity.
| |
GitHub LinkedIn X
4. 26/01/24, 10:35 Gof1 Talk
https://xm1k3.github.io/gof1-talk-ita/ 4/41
STORIA E SVILUPPO DI GOLANG
GoLang, spesso chiamato Go, è stato sviluppato da Google nel 2007. È stato progettato per
migliorare la produttività nella programmazione grazie alla sua semplicità e alla sua
capacità di gestire sistemi di grandi dimensioni.
5. 26/01/24, 10:35 Gof1 Talk
https://xm1k3.github.io/gof1-talk-ita/ 5/41
VANTAGGI DI GOLANG
Go è noto per la sua efficienza e performance, simile al C, ma con una sintassi più pulita.
Supporta la concorrenza, fondamentale nell'era del cloud computing.
6. 26/01/24, 10:35 Gof1 Talk
https://xm1k3.github.io/gof1-talk-ita/ 6/41
CARATTERISTICHE PRINCIPALI DI GOLANG
Le goroutines sono una delle caratteristiche chiave di Go, permettendo la concorrenza
leggera e efficiente. L'uso delle interfacce e una robusta gestione degli errori rendono Go
un linguaggio potente e flessibile.
// Esempio di Goroutine
go func() {
fmt.Println("Esecuzione concorrente")
}()
7. 26/01/24, 10:35 Gof1 Talk
https://xm1k3.github.io/gof1-talk-ita/ 7/41
INTRODUZIONE A GIN GONIC
Gin è un framework web HTTP in Go che offre prestazioni ottimali grazie al suo design
minimalista. È uno dei framework più popolari e veloci per Go.
8. 26/01/24, 10:36 Gof1 Talk
https://xm1k3.github.io/gof1-talk-ita/ 8/41
CARATTERISTICHE DI GIN
Gin fornisce un routing potente, gestione degli errori, middleware e la capacità di creare
API RESTful con facilità. La sua struttura consente di scrivere applicazioni meno verbali e
più efficienti.
9. 26/01/24, 10:36 Gof1 Talk
https://xm1k3.github.io/gof1-talk-ita/ 9/41
ESEMPIO DI ENDPOINT CON GIN
Ecco un semplice esempio di un endpoint API scritto con Gin:
package main
import "github.com/gin-gonic/gin"
func main() {
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
})
r.Run() // Ascolta sulla porta 8080 per default
}
10. 26/01/24, 10:36 Gof1 Talk
https://xm1k3.github.io/gof1-talk-ita/ 10/41
CONFRONTO TRA FRAMEWORK WEB IN GO
🔍Esaminiamo Gin, Echo e Fiber per capire le loro differenze e i punti di forza.
11. 26/01/24, 10:36 Gof1 Talk
https://xm1k3.github.io/gof1-talk-ita/ 11/41
GIN GONIC ✅
Gin è noto per la sua velocità e semplicità. Offre un routing performante, middleware facile
da usare e ottima gestione degli errori.
12. 26/01/24, 10:36 Gof1 Talk
https://xm1k3.github.io/gof1-talk-ita/ 12/41
ECHO
Echo è un framework altamente personalizzabile con funzionalità come il binding
automatico e il rendering di template. Tuttavia, può essere più verboso di Gin.
13. 26/01/24, 10:36 Gof1 Talk
https://xm1k3.github.io/gof1-talk-ita/ 13/41
FIBER
Fiber si ispira a Express.js e punta sulla facilità d'uso. Nonostante sia user-friendly, in
alcuni casi non raggiunge le prestazioni di Gin.
14. 26/01/24, 10:36 Gof1 Talk
https://xm1k3.github.io/gof1-talk-ita/ 14/41
PERCHÉ GIN?
🏆Gin equilibra velocità, facilità d'uso e funzionalità, rendendolo ideale per una vasta
gamma di applicazioni web in Go.
Ecco alcuni dati di benchmark che mostrano le prestazioni di Gin rispetto a Echo e Fiber:
Framework Richieste al secondo Latenza media
Gin 12345 req/s 0.2 ms
Echo 11789 req/s 0.25 ms
Fiber 12001 req/s 0.22 ms
⚡Questi risultati dimostrano la superiore efficienza di Gin in termini di gestione delle
richieste e bassa latenza.
15. 26/01/24, 10:36 Gof1 Talk
https://xm1k3.github.io/gof1-talk-ita/ 15/41
ARCHITETTURA DEL PROGETTO GOF1
20. 26/01/24, 10:36 Gof1 Talk
https://xm1k3.github.io/gof1-talk-ita/ 20/41
INTERFACCE
Le interfacce in Go definiscono le firme per i nostri Service e Repository.
type F1Service interface {
AddDriver(driver models.Driver) error
GetDriver(id int) (models.Driver, error)
GetDrivers(page, limit int) ([]models.Driver, error)
GetDriversByYear(year int) ([]models.Driver, error)
GetDriverStandingsByYear(year int) ([]models.DriverStanding, error)
UpdateDriver(driver models.Driver) error
DeleteDriver(id int) error
ImportDriversFromCsv(record []string) error
...
}
21. 26/01/24, 10:36 Gof1 Talk
https://xm1k3.github.io/gof1-talk-ita/ 21/41
MODELLI
I modelli definiscono le strutture dei dati che andremo ad utilizzare
type Driver struct {
gorm.Model
DriverID int `gorm:"column:id" gorm:"primary_key" csv:"driverId"`
DriverRef string `gorm:"column:driverRef" csv:"driverRef"`
Number string `gorm:"column:number" csv:"number"`
Code string `gorm:"column:code" csv:"code"`
Forename string `gorm:"column:forename" csv:"forename"`
Surname string `gorm:"column:surname" csv:"surname"`
DOB time.Time `gorm:"column:dob" csv:"dob"`
Nationality string `gorm:"column:nationality" csv:"nationality"`
URL string `gorm:"column:url" csv:"url"`
}
...
22. 26/01/24, 10:36 Gof1 Talk
https://xm1k3.github.io/gof1-talk-ita/ 22/41
SERVICE
I Service contengono la logica e interagiscono con i Repository per l'accesso ai dati.
func (s *F1Service) GetDriver(id int) (models.Driver, error) {
return s.Repository.GetDriver(id)
}
23. 26/01/24, 10:36 Gof1 Talk
https://xm1k3.github.io/gof1-talk-ita/ 23/41
REPOSITORY
I Repository sono responsabili dell'interazione diretta con il database, eseguendo query e
aggiornamenti.
func (r *F1Repository) GetDriver(id int) (models.Driver, error) {
var driver models.Driver
r.DB.First(&driver, id)
return driver, nil
}
24. 26/01/24, 10:36 Gof1 Talk
https://xm1k3.github.io/gof1-talk-ita/ 24/41
SERVICE
... ma aumentiamo di poco la complessità...
func (f F1Service) GetDriverStandingsByYear(year int)
([]models.DriverStanding, error) {
if year < 1950 || year > time.Now().Year() {
return nil, fmt.Errorf("year is out of valid range")
}
standings, err := f.Repository.GetDriverStandingsByYear(year)
if err != nil {
return nil, fmt.Errorf("error retrieving driver standings: %w", err)
}
if len(standings) == 0 {
return nil, fmt.Errorf("no driver standings found for year %d", year)
}
return standings, nil
}
35. 26/01/24, 10:36 Gof1 Talk
https://xm1k3.github.io/gof1-talk-ita/ 35/41
TESTS
INTRODUZIONE AI TEST IN GO
I test in Go sono scritti utilizzando il pacchetto `testify`.
La convenzione prevede che i file di test abbiano il suffisso `_test.go`.
36. 26/01/24, 10:36 Gof1 Talk
https://xm1k3.github.io/gof1-talk-ita/ 36/41
STRUTTURA DEL PROGETTO PER I TEST
È comune avere una struttura del progetto separata per i test. Ad esempio:
project/
├── main/
│ ├── main.go
│ └── ...
├── pkg/
│ ├── main.go
│ ├── controller.go
│ ├── service.go
│ └── ...
└── test/
├── main_test.go
├── controller_test.go
├── service_test.go
└── ...
37. 26/01/24, 10:36 Gof1 Talk
https://xm1k3.github.io/gof1-talk-ita/ 37/41
ESECUZIONE DEI TEST
Eseguire i test è semplice utilizzando il comando `go test` dalla radice del progetto:
E modifica i test utilizzando assert:
$ go get github.com/stretchr/testify
import "github.com/stretchr/testify/assert"
func TestExample(t *testing.T) {
assert.Equal(t, 123, 123, "they should be equal")
}
38. 26/01/24, 10:36 Gof1 Talk
https://xm1k3.github.io/gof1-talk-ita/ 38/41
ESEMPIO DI TEST PER IL CONTROLLER
Creiamo un file di test `controller_test.go` per il package `pkg`.
func TestGetDriver(t *testing.T) {
// Setup
controller := NewController(Options{
Database: "test.db",
})
t.Run("GetDriver - Valid ID", func(t *testing.T) {
req, _ := http.NewRequest("GET", "/v1/driver/1", nil)
resp := httptest.NewRecorder()
router := gin.New()
setupRouter(router, controller)
router.ServeHTTP(resp, req)
assert.Equal(t, http.StatusOK, resp.Code)
})
}
40. 26/01/24, 10:36 Gof1 Talk
https://xm1k3.github.io/gof1-talk-ita/ 40/41
GRAZIE
Se avete ulteriori domande o feedback, non esitate a contattarmi.
SEGUIMI SUI SOCIAL
@xm1k3
@mihai-gabriel-canea