Un cours pour se lancer dans la programmation avec le langage Go (golang).
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1 年之前
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. }