Un cours pour se lancer dans la programmation avec le langage Go (golang).
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

92 linhas
1.9 KiB

  1. /* Copyright (C) 2011-2023 Patrick H. E. Foubet - E2L Ivry
  2. Ecole du Logiciel Libre : https://e2li.org/
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or any
  6. later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>
  13. *******************************************************************/
  14. package main
  15. import (
  16. "fmt"
  17. "math"
  18. "net/http"
  19. )
  20. type bob int
  21. func (b bob) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  22. w.Write([]byte("Salut tout le Monde !"))
  23. }
  24. type Circle struct {
  25. Radius float64
  26. }
  27. type Rectangle struct {
  28. Width float64
  29. Height float64
  30. }
  31. type Shape interface {
  32. Area() float64
  33. }
  34. func GetArea(s Shape) float64 {
  35. return s.Area()
  36. }
  37. func (c *Circle) Area() float64 {
  38. return math.Pi * c.Radius * c.Radius
  39. }
  40. func (r Rectangle) Area() float64 {
  41. return r.Height * r.Width
  42. }
  43. func main() {
  44. circle := Circle{5.0}
  45. r1 := Rectangle{4.0, 5.0}
  46. r2 := &Rectangle{10.0, 15.0}
  47. area := circle.Area()
  48. fmt.Println(area)
  49. GetArea(&circle)
  50. GetArea(r1)
  51. GetArea(r2)
  52. var x interface{} = 10
  53. fmt.Println(x)
  54. str, ok := x.(string)
  55. if !ok {
  56. fmt.Println("Ne peut pas convertir l'interface x")
  57. }
  58. fmt.Printf("valeur: %v, Type: %T\n", x, x)
  59. x = 1
  60. fmt.Printf("valeur: %v, Type: %T\n", x, x)
  61. fmt.Printf("valeur: %v, Type: %T\n", str, str)
  62. var y interface{} = "go"
  63. switch v := y.(type) {
  64. case int:
  65. fmt.Printf("Le double de %v vaut %v\n", v, v*2)
  66. case string:
  67. fmt.Printf("%q a une longueur de %v octets\n", v, len(v))
  68. default:
  69. fmt.Printf("Le type de %T est inconnu !\n", v)
  70. }
  71. }