medmond919

Go Example #3

Mar 26th, 2018
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main
  2.  
  3. import "fmt"
  4.  
  5.  
  6. func Map(f func(*), s ...) [] {
  7.     for i := range s {
  8.         f(&s[i])
  9.     }
  10.     return s
  11. }
  12.  
  13. func squareInt(n *int) {
  14.     *n = (*n) * (*n)
  15. }
  16.  
  17. func squareFloat(n *float64) {
  18.     *n = (*n) * (*n)
  19. }
  20.  
  21. func dupliString(s *string) {
  22.     *s = (*s) + (*s)
  23. }
  24.  
  25. func main(){
  26.  
  27.     fmt.Println(Map(squareInt, 1, 2, 3, 4))
  28.     fmt.Println(Map(squareFloat, 1.0, 2.0, 3.0, 4.0))
  29.     fmt.Println(Map(dupliString, "Hello", "world"))
  30.  
  31.  
  32.  
  33. }
Add Comment
Please, Sign In to add comment