Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.65 KB | None | 0 0
  1. // A function can take zero or more arguments.
  2. // In this example, add takes two parameters of type int.
  3. // Type comes after the variable name.
  4.  
  5. package main
  6.  
  7. import "fmt"
  8.  
  9. func add(x int, y int) int {
  10. return x + y
  11. }
  12.  
  13. // Func add can be shortened:
  14. func adds(x, y int) int {
  15. return x + y
  16. }
  17.  
  18. // Multiple returns
  19. func swap(x, y string) (x, y string) {
  20. return x, y
  21. }
  22.  
  23. // Return values may be named.
  24. // If so, they are treated as variables defined at the top of the function.
  25. func split(sum int) (x, y int) {
  26. x = sum * 4 / 9
  27. y = sum - x
  28. return
  29. }
  30.  
  31. func main() {
  32. fmt.Println(add(42, 13))
  33. a, b := swap("Hello", "world")
  34. fmt.Println(a, b)
  35. fmt.Println(split(17))
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement