From 320fff1c6e87c918f32013478b8e24325d0fa8dc Mon Sep 17 00:00:00 2001 From: patrick Date: Mon, 19 Dec 2022 16:16:18 +0100 Subject: [PATCH] Version 0.9 --- boucles/main.go | 61 +++++++++++++++++++++++++++++++++++ conditions/main.go | 47 +++++++++++++++++++++++++++ constantes/main.go | 54 +++++++++++++++++++++++++++++++ fermetures/main.go | 35 ++++++++++++++++++++ interfaces/main.go | 74 +++++++++++++++++++++++++++++++++++++++++++ mots_reserves/main.go | 9 ++++++ operateurs/main.go | 11 +++++++ pointers/main.go | 68 +++++++++++++++++++++++++++++++++++++++ portee/main.go | 68 +++++++++++++++++++++++++++++++++++++++ structures/main.go | 74 +++++++++++++++++++++++++++++++++++++++++++ variables/main.go | 24 ++++++++++++++ 11 files changed, 525 insertions(+) create mode 100644 boucles/main.go create mode 100644 conditions/main.go create mode 100644 constantes/main.go create mode 100644 fermetures/main.go create mode 100644 interfaces/main.go create mode 100644 mots_reserves/main.go create mode 100644 operateurs/main.go create mode 100644 pointers/main.go create mode 100644 portee/main.go create mode 100644 structures/main.go create mode 100644 variables/main.go diff --git a/boucles/main.go b/boucles/main.go new file mode 100644 index 0000000..3e5b083 --- /dev/null +++ b/boucles/main.go @@ -0,0 +1,61 @@ +package main + +import "fmt" + +func main() { + for i := 0; i < 5; i++ { + fmt.Println(i) + } + + for i := 0; ; { + if i >= 5 { + break + } + fmt.Println(i) + i++ + } + + i := 0 + for i < 5 { + fmt.Println(i) + i++ + } + + i = 0 + for ; ; i++ { + if i >= 5 { + break + } + fmt.Println(i) + } + + for i := 0; i < 5; { + fmt.Println(i) + i++ + } + + i = 0 + for ; i < 5; i++ { + fmt.Println(i) + } + + for i := 0; ; i++ { + if i >= 5 { + break + } + fmt.Println(i) + } + + i = 0 + for { + if i >= 5 { + break + } + fmt.Println(i) + i++ + } + + for i, j := 0, 10; i != j; i, j = i+1, j-1 { + fmt.Println(i, j) + } +} diff --git a/conditions/main.go b/conditions/main.go new file mode 100644 index 0000000..47a6468 --- /dev/null +++ b/conditions/main.go @@ -0,0 +1,47 @@ +package main + +import ( + "fmt" + "math/rand" + "time" +) + +func main() { + var x = 1 + if x > 5 { + fmt.Println("x est superieur a 5") + } + + for i := 1; i < 100; i++ { + fizzbuzz(i) + } + // Decommenter cette ligne pour avoir l'erreur depassement de capacite + // fmt.Println(minRand(5000000000000000000)) +} + +func fizzbuzz(n int) { + if n%15 == 0 { + fmt.Println("FizzBuzz") + } else if (n % 5) == 0 { + fmt.Println("Buzz") + } else if (n % 3) == 0 { + fmt.Println("Fizz") + } else { + fmt.Println(n) + } +} + +func minRand(min int) int { + rand.Seed(time.Now().UnixNano()) + if v := rand.Int(); v > min { + return v + } + return min +} + +func isEven(n int) bool { + if n%2 == 1 { + return false + } + return true +} diff --git a/constantes/main.go b/constantes/main.go new file mode 100644 index 0000000..0afdbda --- /dev/null +++ b/constantes/main.go @@ -0,0 +1,54 @@ +package main + +import ( + "fmt" + "math" +) + +var User string = "E2L !" + +type person struct { + int +} + +type aint int + +const ( + BigConst = math.MaxFloat64 * math.MaxFloat64 + data aint = 10 +) + +const ( + a0 = iota // 0 + a1 = iota // 1 + a2 = iota // 2 + a3 = iota // 3 +) + +const ( + b0 = iota // 0 + b1 // 1 + b2 // 2 + b3 // 3 +) + +const ( + c0 = iota // 0 + c1 = 43 + c2 = 75 + c3 = iota // 3 +) + +const d0 = iota //0 +const d1 = iota //0 +const d2 = iota //0 +const d3 = iota //0 + +func main() { + fmt.Println(a0, a1, a2, a3) + fmt.Println(b0, b1, b2, b3) + fmt.Println(c0, c1, c2, c3) + fmt.Println(d0, d1, d2, d3) + fmt.Println("Vive " + User); +} + diff --git a/fermetures/main.go b/fermetures/main.go new file mode 100644 index 0000000..b729a31 --- /dev/null +++ b/fermetures/main.go @@ -0,0 +1,35 @@ +package main + +import ( + "fmt" +) + +func main() { + adder := func(a int, b int) int { + return a + b + } + + fmt.Println(adder(5, 6)) + + counter1 := counter() + counter2 := counter() + + fmt.Println(counter1()) + fmt.Println(counter2()) + + deferredPrint() +} + +func counter() func() int { + i := 0 + return func() int { + i++ + return i + } +} + +func deferredPrint() { + defer fmt.Println("defer 1") + fmt.Println("regular thing") + defer fmt.Println("defer 2") +} diff --git a/interfaces/main.go b/interfaces/main.go new file mode 100644 index 0000000..82236d0 --- /dev/null +++ b/interfaces/main.go @@ -0,0 +1,74 @@ +package main + +import ( + "fmt" + "math" + "net/http" +) + +type bob int + +func (b bob) ServeHTTP(w http.ResponseWriter, r *http.Request) { + w.Write([]byte("Salut tout le Monde !")) +} + +type Circle struct { + Radius float64 +} + +type Rectangle struct { + Width float64 + Height float64 +} + +type Shape interface { + Area() float64 +} + +func GetArea(s Shape) float64 { + return s.Area() +} + +func (c *Circle) Area() float64 { + return math.Pi * c.Radius * c.Radius +} + +func (r Rectangle) Area() float64 { + return r.Height * r.Width +} + +func main() { + circle := Circle{5.0} + r1 := Rectangle{4.0, 5.0} + r2 := &Rectangle{10.0, 15.0} + area := circle.Area() + fmt.Println(area) + GetArea(&circle) + GetArea(r1) + GetArea(r2) + + var x interface{} = 10 + fmt.Println(x) + + str, ok := x.(string) + if !ok { + fmt.Println("Ne peut pas convertir l'interface x") + } + + fmt.Printf("valeur: %v, Type: %T\n", x, x) + x = 1 + fmt.Printf("valeur: %v, Type: %T\n", x, x) + + fmt.Printf("valeur: %v, Type: %T\n", str, str) + + var y interface{} = "go" + switch v := y.(type) { + case int: + fmt.Printf("Le double de %v vaut %v\n", v, v*2) + case string: + fmt.Printf("%q a une longueur de %v octets\n", v, len(v)) + default: + fmt.Printf("Le type de %T est inconnu !\n", v) + } +} + diff --git a/mots_reserves/main.go b/mots_reserves/main.go new file mode 100644 index 0000000..c6d1c6b --- /dev/null +++ b/mots_reserves/main.go @@ -0,0 +1,9 @@ +package main + +import "fmt" + +func main() { + int := 10 + // var c int = 10 + fmt.Println(int) +} diff --git a/operateurs/main.go b/operateurs/main.go new file mode 100644 index 0000000..cf75cdc --- /dev/null +++ b/operateurs/main.go @@ -0,0 +1,11 @@ +package main + +import "fmt" + +func main() { + x := 5*10 + 6/3 | 7%4&15 // same as (5*10) + (6/3) | (7%4)&15 = 50 + 2 | 3&15 = 52 | 3 = + fmt.Println(x) + + a := true && false || true && !false + fmt.Println(a) +} diff --git a/pointers/main.go b/pointers/main.go new file mode 100644 index 0000000..17ccfb9 --- /dev/null +++ b/pointers/main.go @@ -0,0 +1,68 @@ +package main + +import "fmt" + +func main() { + i := 10 + fmt.Println(&i) + n := &i + fmt.Printf("%T\n", n) + fmt.Println(n) + fmt.Println(i) + *n = 15 + fmt.Println(i) + + j := 5 + n = &j + *n = 6 + + fmt.Println(i) + + a := 5 + fmt.Println(a) // value is 5 + pointer(&a) + fmt.Println(a) // value is 10 + a = 5 + fmt.Println(a) // value is 5 + value(a) + fmt.Println(a) // value is still 5 + + ar := []int{1, 2, 3, 4} + fmt.Println(ar) + arrval(ar) + fmt.Println(ar) + arrpointer(&ar) + fmt.Println(ar) + + m := make(map[int]int) + m[1] = 1 + m[2] = 2 + m[3] = 3 + + fmt.Println(m) + mapped(m) + fmt.Println(m) + m2 := map[int]int{1: 1} + mapped(m2) + fmt.Println(m2) +} + +func pointer(x *int) { + *x = 10 +} + +func value(x int) { + x = 10 +} + +func arrval(a []int) { + a = append(a, 5) +} + +func arrpointer(a *[]int) { + *a = append(*a, 5) +} + +func mapped(m map[int]int) { + m[4] = 4 +} diff --git a/portee/main.go b/portee/main.go new file mode 100644 index 0000000..861a407 --- /dev/null +++ b/portee/main.go @@ -0,0 +1,68 @@ +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) + +} + diff --git a/structures/main.go b/structures/main.go new file mode 100644 index 0000000..b78c9f0 --- /dev/null +++ b/structures/main.go @@ -0,0 +1,74 @@ +package main + +import ( + "fmt" + "math" +) + +type Wheel struct { + Circle + Material string + Color string + X float64 +} + +func (w Wheel) Info() { + fmt.Printf("Une roue est faite en %s de couleur %s\n", w.Material, w.Color) +} + +type Circle struct { + X float64 + Y float64 + Radius float64 +} + +type Shape interface { + Area() float64 + Area2() float64 +} + +func (c *Circle) Area() float64 { + return c.Radius * c.Radius * math.Pi +} + +func (c Circle) Area2() float64 { + return c.Radius * c.Radius * math.Pi +} + +func Area(c Circle) float64 { + return c.Radius * c.Radius * math.Pi +} + +func ShapeArea(s Shape) float64 { + return s.Area() +} + +func main() { + c1 := Circle{ + X: 15.0, + Y: 12.0, + Radius: 8.5, + } + + c2 := Circle{15.0, 12, 8.5} + + fmt.Println(c1 == c2) + + fmt.Println(c1.Area() == c1.Area2()) + + w1 := Wheel{ + Circle: Circle{ + Radius: 10.0, + X: 15.0, + }, + Material: "Caoutchouc", + Color: "Noire", + X: 5.0, + } + w1.Info() + + fmt.Println(w1.Area()) + fmt.Println(w1.X) + fmt.Println(w1.Circle.X) +} + diff --git a/variables/main.go b/variables/main.go new file mode 100644 index 0000000..f5a2d55 --- /dev/null +++ b/variables/main.go @@ -0,0 +1,24 @@ +package main + +import "fmt" + +var a = 10 +var b int8 = 122 +var c, d = 100, 200 + +const PI = 100 + +func main() { + var a = 10 + var b = 1.5 + var c byte = 'c' + var d = true + var e = '😀' + var f = "hello" + var g = 1 + 3i + + var _1åœπbool = 10 + + fmt.Printf("%T\t%T\t%T\t%T\t%T\t%T\t%T\t%T\n", a, b, c, d, e, f, g, _1åœπbool) + +}