-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
41 lines (31 loc) · 1.14 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package main
import "fmt"
func main() {
// Déclaration d'une chaine de caractères
ch := "Bonjour le monde"
fmt.Println(ch)
// Une chaine littérale se déclare avec une back quote et permet de spécifier
// tous les caractères qu'on veut même les sauts de ligne
sch := `Bonjour
le monde`
fmt.Println(sch) // affiche la chaine avec le saut de ligne
// Les chaines sont immutables = on ne peut pas les modifier
//ch[2] = 'X' // va générer une erreur de compilation "cannot assign to ch[2]"
// Sous-chaines
sch = ch[1:3] // ch[d:f] d l'indice du début inclu, f l'indice de fin exclu
fmt.Println(sch) // "on"
sch = ch[:4] // on peut omêtre les indices, ici on part de 0
fmt.Println(sch) // "Bonj"
sch = ch[8:] // la on va jusqu'à la fin = len(ch)
fmt.Println(sch) // "le monde !"
sch = ch[:] // ça sert à rien mais c'est fun
fmt.Println(sch)
// On peut parcourir une chaine grâce à "range" qui tient compte des runes
// Un rune c'est un caractère encodé en UTF-8
for i, e := range ch {
fmt.Printf("%d\t%q\n", i, e)
}
// On concatène 2 chaines avec l'addition
sch = ch + " et aurevoir ...."
fmt.Println(sch)
}