Advertisement
AntonioVillanueva

Test types génériques dans Go

Mar 31st, 2023
968
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.63 KB | None | 0 0
  1. // Test types génériques en go goland A.Villanueva
  2. package main
  3.  
  4. import "fmt"
  5.  
  6. func Swap[T any](a *T, b *T) {
  7.     /*Fonction générique pour effectuer un Swap
  8.     *Peut travailler avec différents types de données
  9.      */
  10.     temp := *a
  11.     *a = *b
  12.     *b = temp
  13. }
  14.  
  15. func PrintDeux[T any](a *T, b *T) {
  16.     /*Fonction générique pour effectuer un Print double
  17.     *Peut travailler avec différents types de données
  18.      */
  19.     fmt.Println(*a, ",", *b)
  20. }
  21.  
  22. func main() {
  23.     a := 1 //Int
  24.     b := 2
  25.  
  26.     PrintDeux(&a, &b)
  27.     Swap(&a, &b)
  28.     PrintDeux(&a, &b)
  29.  
  30.     c := "one" //String
  31.     d := "two"
  32.  
  33.     PrintDeux(&c, &d)
  34.     Swap(&c, &d)
  35.     PrintDeux(&c, &d)
  36.  
  37. }
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement