Bläddra i källkod

Version 0.9

master
Patrick Foubet 1 år sedan
förälder
incheckning
320fff1c6e
11 ändrade filer med 525 tillägg och 0 borttagningar
  1. +61
    -0
      boucles/main.go
  2. +47
    -0
      conditions/main.go
  3. +54
    -0
      constantes/main.go
  4. +35
    -0
      fermetures/main.go
  5. +74
    -0
      interfaces/main.go
  6. +9
    -0
      mots_reserves/main.go
  7. +11
    -0
      operateurs/main.go
  8. +68
    -0
      pointers/main.go
  9. +68
    -0
      portee/main.go
  10. +74
    -0
      structures/main.go
  11. +24
    -0
      variables/main.go

+ 61
- 0
boucles/main.go Visa fil

@@ -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)
}
}

+ 47
- 0
conditions/main.go Visa fil

@@ -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
}

+ 54
- 0
constantes/main.go Visa fil

@@ -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);
}


+ 35
- 0
fermetures/main.go Visa fil

@@ -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")
}

+ 74
- 0
interfaces/main.go Visa fil

@@ -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)
}
}


+ 9
- 0
mots_reserves/main.go Visa fil

@@ -0,0 +1,9 @@
package main

import "fmt"

func main() {
int := 10
// var c int = 10
fmt.Println(int)
}

+ 11
- 0
operateurs/main.go Visa fil

@@ -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)
}

+ 68
- 0
pointers/main.go Visa fil

@@ -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
}

+ 68
- 0
portee/main.go Visa fil

@@ -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)

}


+ 74
- 0
structures/main.go Visa fil

@@ -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)
}


+ 24
- 0
variables/main.go Visa fil

@@ -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)

}

Laddar…
Avbryt
Spara