Advertisement
fcamuso

Un assaggio di Go - Video 5

Jul 2nd, 2025
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.14 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "bufio"
  5.     "fmt"
  6.     "os"
  7. )
  8.  
  9. func main() {
  10.  
  11.     x := 11
  12.  
  13.     if x > 10 { // Corretto
  14.         fmt.Println("x is greater than 10")
  15.     } else {
  16.         fmt.Println("x is lower than 2")
  17.     }
  18.  
  19.     // Errato
  20.     // if (x > 10) {
  21.     //      fmt.Println("x is greater than 10")
  22.     // }
  23.  
  24.     if x := getData(); x > 10 {
  25.         fmt.Println(x * 2)
  26.     }
  27.  
  28.     lingua := "Spagnolo"
  29.  
  30.     switch lingua {
  31.     case "Italiano":
  32.         fmt.Println("Salve!")
  33.     case "Inglese", "Inglese US", "Inglese BT":
  34.         fmt.Println("Wellcome!")
  35.     default:
  36.         fmt.Println("Lingua non supportata")
  37.     }
  38.  
  39.     coefficiente := 7
  40.     switch coefficiente % 5 {
  41.     case 0:
  42.         //istruzioni
  43.     case 2:
  44.         //istruzioni
  45.         fallthrough
  46.     case 3:
  47.         //istruzioni
  48.     case 4:
  49.         //istruzioni
  50.     }
  51.  
  52.     var persona Utente = Insegnante{"fcamuso", "Informatica"}
  53.  
  54.     switch v := persona.(type) {
  55.     case Studente:
  56.         fmt.Printf("Studente: %s, classe: %s\n", v.nome, v.classe)
  57.     case Insegnante:
  58.         fmt.Printf("Insegnante: %s, materia: %s\n", v.nome, v.materia)
  59.     default:
  60.         fmt.Println("Utente sconosciuto")
  61.     }
  62.  
  63.     for i := 0; i < 10; i++ {
  64.         fmt.Println(i)
  65.  
  66.         file, _ := os.Open("main.go")
  67.         defer file.Close()
  68.  
  69.         scanner := bufio.NewScanner(file)
  70.  
  71.         // "while scanner.Scan() { ... }"
  72.         for scanner.Scan() {
  73.             riga := scanner.Text()
  74.             fmt.Println(riga)
  75.         }
  76.  
  77.     var scelta int
  78.     for {
  79.                     // Corpo del ciclo: viene eseguito almeno una volta
  80.                     fmt.Println("Menu:")
  81.                     fmt.Println("1. Opzione uno")
  82.                     fmt.Println("2. Opzione due")
  83.                     fmt.Println("0. Esci")
  84.                     fmt.Print("Scegli un'opzione: ")
  85.  
  86.                     fmt.Scanln(&scelta)
  87.  
  88.                     //switch in base a scelta ...
  89.  
  90.                     if scelta == 0 {
  91.                             break // questo esce dal ciclo for
  92.                     }
  93.  
  94.                     fmt.Println() // riga vuota tra un ciclo e l’altro
  95.             }
  96.            
  97.         }
  98. }
  99.  
  100. //y = x > 0 ? -x : 2*x;
  101.  
  102. // Interfaccia comune
  103. type Utente interface {
  104.     Nome() string
  105. }
  106.  
  107. // Tipo Insegnante
  108. type Insegnante struct {
  109.     nome    string
  110.     materia string
  111. }
  112.  
  113. func (i Insegnante) Nome() string {
  114.     return i.nome
  115. }
  116.  
  117. // Tipo Studente
  118. type Studente struct {
  119.     nome   string
  120.     classe string
  121. }
  122.  
  123. func (s Studente) Nome() string {
  124.     return s.nome
  125. }
  126.  
  127. func getData() int { return 900 }
  128.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement