Advertisement
AntonioVillanueva

RESTFUL server in Go

Mar 30th, 2023
1,836
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.17 KB | Software | 0 0
  1. //Antonio Villanueva Test Restful Server
  2. package main
  3.  
  4. import (
  5.         "net/http"
  6.         //go get github.com/gin-gonic/gin
  7.         "github.com/gin-gonic/gin"
  8. )
  9.  
  10. // Type Structure représentant un salarié
  11. type employe struct {
  12.     ID     int `json:"id"`
  13.     Prenom  string  `json:"prenom"`
  14.     Nom string  `json:"nom"`
  15.     Job  string `json:"job"`
  16. }
  17.  
  18. // employés de l'entreprise []
  19. var employees = []employe{
  20.     {ID:1, Prenom:"Tony", Nom:"Villanueva", Job:"développeur"},
  21.     {ID:2,Prenom:"Gilles", Nom:"Pignatta", Job:"développeur"},
  22.     {ID:3,Prenom:"Frank", Nom:"Clerissi", Job:"chargé d'affaires"},
  23.     {ID:4,Prenom:"Alex", Nom:"Zucarelli", Job:"designer"},
  24.     {ID:5,Prenom:"Damien ", Nom:"LeGoffre", Job:"technique"},              
  25. }
  26.  
  27. //Requête GET http://localhost;8080/employees renvoie la liste des employés au format JSON
  28. func getEmployees(c *gin.Context) {
  29.     c.IndentedJSON(http.StatusOK, employees)
  30. }
  31.  
  32. //Requête GET http://localhost;8080/
  33. func getDefault(c *gin.Context) {
  34.     c.IndentedJSON(http.StatusOK, "Icaro Restful web")
  35. }
  36.  
  37. func main() {
  38.     router := gin.Default()
  39.    
  40.     router.GET("/employees", getEmployees)
  41.     router.GET("/", getDefault)
  42.    
  43.     router.Run("localhost:8080")
  44. }
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement