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.

154 lines
3.2 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 (
  16. "fmt"
  17. "strconv"
  18. )
  19. type myFloat = float64
  20. type myint int
  21. // func (m myFloat) double() myFloat {
  22. // }
  23. type person struct {
  24. age int
  25. name string
  26. }
  27. type student struct {
  28. age int // try swapping these fields
  29. name string
  30. }
  31. func (m myint) double() myint {
  32. return m * 2
  33. }
  34. func main() {
  35. var a uint8 = 1
  36. var b uint16 = 2
  37. var c uint32 = 3
  38. var d uint64 = 4
  39. var e int8 = 1
  40. var f int16 = 2
  41. var g int32 = 3
  42. var h int64 = 4
  43. var i float32 = 1.1
  44. var j float64 = 1.2
  45. var k bool = true
  46. var l byte = 'c'
  47. var m byte = 99
  48. var n rune = '😀'
  49. var o rune = 128512
  50. var p string = "hello"
  51. var arr = [5]int{}
  52. var slice = []int{}
  53. var maps = map[int]int{}
  54. type empty struct{}
  55. var x = empty{}
  56. var y interface{} = x
  57. var function = func() int {
  58. return 42
  59. }
  60. var aliasInt myint = 1
  61. var aliasFloat myFloat = 5.0
  62. fmt.Printf("Type of a=%T, b=%T, c=%T, d=%T\n", a, b, c, d)
  63. fmt.Printf("Type of e=%T, f=%T, g=%T, h=%T\n", e, f, g, h)
  64. fmt.Printf("Type of i=%T, j=%T\n", i, j)
  65. fmt.Printf("Type of k=%T\n", k)
  66. fmt.Printf("Type of l=%T, m=%T, n=%T, o=%T\n", l, m, n, o)
  67. fmt.Printf("Type of p=%T\n", p)
  68. fmt.Printf("Type of arr=%T\n", arr)
  69. fmt.Printf("Type of arr=%T\n", slice)
  70. fmt.Printf("Type of arr=%T\n", maps)
  71. fmt.Printf("Type of x=%T\n", x)
  72. fmt.Printf("Type of y=%T\n", y)
  73. fmt.Printf("Type of function=%T\n", function)
  74. fmt.Printf("Type of aliasInt=%T, aliasFloat=%T\n", aliasInt, aliasFloat)
  75. conversion()
  76. definedTypeConversion(aliasInt)
  77. definedTypeConversion(5)
  78. takesInt(5)
  79. // takesInt(aliasInt) // this does not work
  80. takesFloat(5)
  81. takeInt64(5)
  82. takeInt8(5) // try passing > 128
  83. }
  84. func conversion() {
  85. var i int = 355 // type int
  86. f := float64(i) // type float64
  87. b := i != 0 // type bool
  88. s := strconv.Itoa(i)
  89. by := byte(i)
  90. ui8 := 250
  91. i8 := int8(ui8)
  92. fmt.Printf("%d\t%f\t%t\t%s\t%c\t%d\t%d\n", i, f, b, s, by, ui8, i8)
  93. p := person{age: 20, name: "john"}
  94. std := student{age: 15, name: "paul"}
  95. p2 := person(std)
  96. s2 := student(p)
  97. // p2 := *(*person)(unsafe.Pointer(&std))
  98. // s2 := *(*student)(unsafe.Pointer(&p))
  99. fmt.Printf("Type of p=%T, std=%T, p2=%T, s2=%T\n", p, std, p2, s2)
  100. }
  101. func definedTypeConversion(mi myint) {
  102. fmt.Printf("Type of mi=%T\n", mi)
  103. }
  104. func takesInt(i int) {
  105. fmt.Printf("Type of i=%T\n", i)
  106. }
  107. func takesFloat(f float32) {
  108. fmt.Printf("Type of i=%T\n", f)
  109. }
  110. func takeInt8(i int8) {
  111. fmt.Printf("Type of i=%T\n", i)
  112. }
  113. func takeInt64(i int64) {
  114. fmt.Printf("Type of i=%T\n", i)
  115. }