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.

86 lines
1.5 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 "fmt"
  16. type person struct {
  17. name string
  18. }
  19. var p = person{name: "R Stallman"}
  20. func main() {
  21. p1 := person{name: "Jane Doe"} // ici person definit en global
  22. type person struct {
  23. name string
  24. age int
  25. }
  26. p2 := person{ // ici person en local
  27. name: "John Doe",
  28. age: 27,
  29. }
  30. fmt.Printf("Type de p: %+v\tp1: %v\tp2: %v\n", p, p1, p2)
  31. blocks()
  32. scopes()
  33. shadowing()
  34. }
  35. func blocks() {
  36. i := 10
  37. {
  38. i := 5
  39. fmt.Println(i) // i vaut 5
  40. }
  41. fmt.Println(i) // i vaut 10
  42. }
  43. var y = 100
  44. func scopes() {
  45. x := 10
  46. var z int
  47. {
  48. fmt.Println(x)
  49. y := 15
  50. fmt.Println(y)
  51. z = 20
  52. }
  53. fmt.Println(z)
  54. fmt.Println(y)
  55. }
  56. func shadowing() {
  57. x := 10
  58. {
  59. x := 15
  60. {
  61. x := 20
  62. fmt.Println(x)
  63. }
  64. fmt.Println(x)
  65. }
  66. fmt.Println(x)
  67. }