Un cours pour se lancer dans la programmation avec le langage Go (golang).
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

main.go 1.9 KiB

1 år sedan
1 år sedan
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. }