-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathfunctions.go
68 lines (54 loc) · 1.95 KB
/
functions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import "fmt"
// Here’s a function that takes two ints and returns their sum as an int.
func plus(a int, b int) int {
// Go requires explicit returns, i.e. it won’t automatically return the value of the last expression.
return a + b
}
// When you have multiple consecutive parameters of the same type,
// you may omit the type name for the like-typed parameters up to the final parameter that declares the type.
func plusPlus(a, b, c int) int {
return a + b + c
}
// -------------------------------------------------------
// Go has built-in support for multiple return values.
// This feature is used often in idiomatic Go,
// for example to return both result and error values from a function.
// The (int, int) in this function signature shows that the function returns 2 ints.
func vals() (int, int) {
return 3, 7
}
// -------------------------------------------------------
// Variadic functions can be called with any number of trailing arguments.
// For example, fmt.Println is a common variadic function.
// Here’s a function that will take an arbitrary number of ints as arguments.
func sum(nums ...int) {
fmt.Print("Variadic", " ")
total := 0
for _, num := range nums {
total += num
}
fmt.Println(total)
}
// Functions details functions in golang
func Functions() {
// Call a function just as you’d expect, with name(args).
res := plus(1, 2)
fmt.Println("1+2 = ", res)
res = plusPlus(1, 2, 3)
fmt.Println("1+2+3 = ", res)
// Here we use the 2 different return values from the call with multiple assignment.
a, b := vals()
fmt.Println(a)
fmt.Println(b)
// If you only want a subset of the returned values, use the blank identifier _.
_, c := vals()
fmt.Println(c)
// Variadic functions can be called in the usual way with individual arguments.
sum(1, 2)
sum(1, 2, 3)
// If you already have multiple args in a slice,
// apply them to a variadic function using func(slice...) like this.
nums := []int{1, 2, 3, 4}
sum(nums...)
}