Un cours pour se lancer dans la programmation avec le langage Go (golang).
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

75 wiersze
1.2 KiB

  1. package main
  2. import (
  3. "fmt"
  4. "math"
  5. "net/http"
  6. )
  7. type bob int
  8. func (b bob) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  9. w.Write([]byte("Salut tout le Monde !"))
  10. }
  11. type Circle struct {
  12. Radius float64
  13. }
  14. type Rectangle struct {
  15. Width float64
  16. Height float64
  17. }
  18. type Shape interface {
  19. Area() float64
  20. }
  21. func GetArea(s Shape) float64 {
  22. return s.Area()
  23. }
  24. func (c *Circle) Area() float64 {
  25. return math.Pi * c.Radius * c.Radius
  26. }
  27. func (r Rectangle) Area() float64 {
  28. return r.Height * r.Width
  29. }
  30. func main() {
  31. circle := Circle{5.0}
  32. r1 := Rectangle{4.0, 5.0}
  33. r2 := &Rectangle{10.0, 15.0}
  34. area := circle.Area()
  35. fmt.Println(area)
  36. GetArea(&circle)
  37. GetArea(r1)
  38. GetArea(r2)
  39. var x interface{} = 10
  40. fmt.Println(x)
  41. str, ok := x.(string)
  42. if !ok {
  43. fmt.Println("Ne peut pas convertir l'interface x")
  44. }
  45. fmt.Printf("valeur: %v, Type: %T\n", x, x)
  46. x = 1
  47. fmt.Printf("valeur: %v, Type: %T\n", x, x)
  48. fmt.Printf("valeur: %v, Type: %T\n", str, str)
  49. var y interface{} = "go"
  50. switch v := y.(type) {
  51. case int:
  52. fmt.Printf("Le double de %v vaut %v\n", v, v*2)
  53. case string:
  54. fmt.Printf("%q a une longueur de %v octets\n", v, len(v))
  55. default:
  56. fmt.Printf("Le type de %T est inconnu !\n", v)
  57. }
  58. }