|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- package main
-
- import "fmt"
-
- type person struct {
- name string
- }
-
- var p = person{name: "R Stallman"}
-
- func main() {
- p1 := person{name: "Jane Doe"} // ici person definit en global
-
- type person struct {
- name string
- age int
- }
-
- p2 := person{ // ici person en local
- name: "John Doe",
- age: 27,
- }
-
- fmt.Printf("Type de p: %+v\tp1: %v\tp2: %v\n", p, p1, p2)
- blocks()
- scopes()
- shadowing()
- }
-
- func blocks() {
- i := 10
- {
- i := 5
- fmt.Println(i) // i vaut 5
- }
- fmt.Println(i) // i vaut 10
- }
-
- var y = 100
-
- func scopes() {
- x := 10
- var z int
- {
- fmt.Println(x)
- y := 15
- fmt.Println(y)
- z = 20
- }
- fmt.Println(z)
- fmt.Println(y)
-
- }
-
- func shadowing() {
- x := 10
- {
- x := 15
- {
- x := 20
- fmt.Println(x)
- }
- fmt.Println(x)
- }
- fmt.Println(x)
-
- }
-
|