Advertisement
fcamuso

Un assaggio di Go - Video 8

Jul 5th, 2025
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 3.15 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "errors"
  5.     "fmt"
  6. )
  7.  
  8. type Indirizzo struct {
  9.     Citta string
  10.     CAP   string
  11. }
  12.  
  13. type Persona struct {
  14.     Nome    string
  15.     Cognome string
  16.     Eta     int
  17.  
  18.     Indirizzo
  19.     //codice string
  20. }
  21.  
  22. func (p *Persona) Invecchia() {
  23.     p.Eta++
  24.     //((*p).Eta)++
  25. }
  26.  
  27. func NewPersona(nome string, cognome string, eta int) (*Persona, error) {
  28.     if nome == "" || cognome == "" {
  29.         return nil, errors.New("nome/cognome non possono essere vuoti")
  30.     }
  31.     if eta < 0 || eta > 130 {
  32.         return nil, fmt.Errorf("età %d non valida", eta)
  33.     }
  34.  
  35.     p := &Persona{
  36.         Nome:    nome,
  37.         Cognome: cognome,
  38.         Eta:     eta,
  39.     }
  40.     return p, nil
  41. }
  42.  
  43. // Dipendente "compone" Persona
  44. type Dipendente struct {
  45.     Persona  // embedding
  46.     Mansione string
  47. }
  48.  
  49. // Dirigente "compone" Persona
  50. type Dirigente struct {
  51.     Persona // embedding
  52.     Livello string
  53. }
  54.  
  55. // Interfaccia che definisce il comportamento comune
  56. type BonusCalcolabile interface {
  57.     BonusAnzianita() int
  58. }
  59.  
  60. // Implementazione del metodo BonusAnzianita per Dipendente
  61. func (d Dipendente) BonusAnzianita() int {
  62.     anni := d.Eta - 20
  63.     if anni < 0 {
  64.         return 0
  65.     }
  66.     return anni * 250
  67. }
  68.  
  69. // Implementazione del metodo BonusAnzianita per Dirigente
  70. func (d Dirigente) BonusAnzianita() int {
  71.     anni := d.Eta - 30
  72.     if anni < 0 {
  73.         return 0
  74.     }
  75.     return anni * 1000
  76. }
  77.  
  78. func main() {
  79.  
  80.     dip := Dipendente{
  81.         Persona: Persona{
  82.             Nome:    "Luca",
  83.             Cognome: "Verdi",
  84.             Eta:     35,
  85.         },
  86.         Mansione: "Tecnico",
  87.     }
  88.  
  89.     dir := Dirigente{
  90.         Persona: Persona{
  91.             Nome:    "Anna",
  92.             Cognome: "Bianchi",
  93.             Eta:     50,
  94.         },
  95.         Livello: "Senior",
  96.     }
  97.  
  98.     // Uso dell'interfaccia
  99.     var lista []BonusCalcolabile = []BonusCalcolabile{dip, dir} //conformità di tipo!
  100.  
  101.     for _, p := range lista {
  102.         fmt.Println("Bonus per ")
  103.         switch v := p.(type) {
  104.         case Dipendente:
  105.             fmt.Println(v.Cognome, " ", v.Nome, " Mansione: ", v.Mansione)
  106.         case Dirigente:
  107.             fmt.Println(v.Cognome, " ", v.Nome, " Livello: ", v.Livello)
  108.         default:
  109.             fmt.Println("Ruolo sconosciuto")
  110.         }
  111.  
  112.         fmt.Println("Bonus anzianita", p.BonusAnzianita())
  113.     }
  114.  
  115.     // p1 := Persona{}
  116.  
  117.     // var p2 = Persona{
  118.     //  Nome:    "Mario",
  119.     //  Cognome: "Rossi",
  120.     //  Eta:     30,
  121.     // }
  122.  
  123.     // p2.Invecchia() //Invecchia(p2)
  124.  
  125.     // p3, err := NewPersona("Mario", "Rossi", 50)
  126.  
  127.     // if err != nil {
  128.     //  fmt.Println("Errore creando p1:", err)
  129.     // } else {
  130.     //  fmt.Println("Persona creata:", *p3)
  131.     // }
  132.  
  133.     // fmt.Println(p1, p2)
  134.  
  135.     // p2.Citta = "Milano";
  136.  
  137.     // map1 := make(map[string]int)
  138.     // map1["Tevere"] = 406
  139.  
  140.     // map2 := map[string]float32{
  141.     //  "standard":     69.99,
  142.     //  "deluxe":       89,
  143.     //  "super deluxe": 129,
  144.     // }
  145.     // map2["stra super deluxe"] = 200
  146.  
  147.     // //ha la deluxe?
  148.     // costo, esiste := map2["deluxe"]
  149.  
  150.     // if !esiste {
  151.     //  fmt.Println("Edizione non disponibile")
  152.     // } else {
  153.     //  fmt.Println("Costo: ", costo)
  154.     // }
  155.  
  156.     // delete(map2, "deluxe")
  157.     // costo, esiste = map2["deluxe"]
  158.  
  159.     // if !esiste {
  160.     //  fmt.Println("Edizione non disponibile")
  161.     // } else {
  162.     //  fmt.Println("Costo: ", costo)
  163.     // }
  164.  
  165.     // //iteriamo sulla mappa
  166.     // for key, value := range map2 {
  167.     //  fmt.Println(key, " Costo: ", value)
  168.     // }
  169.  
  170. }
  171.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement