-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbasics.go
62 lines (43 loc) · 1.36 KB
/
basics.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
package main
import (
"fmt"
"github.com/pkg/errors"
)
func main() {
}
func privateFunction() {
//Functions with a lowercase character as the first character in the function name are private to the current package
}
func PublicFunction() {
//Functions with an uppercase character as the first character in the function name are public and can be imported and
//used in other packages
}
func Params(a int, b string) {
//Parameter names come before the type
}
func SameParams(a, b, c int) {
// Only need to specify the type once in functions
}
func SingleReturn() bool {
//Return types come at the end of the function signature
return true
}
func MultiReturn() (bool, error) {
//You can return n values from a function
return false, nil
}
func NamedReturn() (b bool, err error) {
// You can name your return values, but this is only really done to add clarity to GoDoc or to save a variable declaration
// and less commonly to allow defer statements to alter return values (see defer.go in this folder)
b = true
err = errors.New("Error message")
// You still need to explicitly return
return b, err
}
func CallingMultiReturnValueFunctions() {
result, err := NamedReturn()
fmt.Printf("%v %v\n", result, err)
//You can ignore return values with the 'blank identifier' _
result, _ = MultiReturn()
// IF YOU ARE IGNORING AN ERROR, YOU ARE DOING SOMETHING WRONG
}