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.

main.go 2.4 KiB

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