-
Notifications
You must be signed in to change notification settings - Fork 2
/
example3.go
54 lines (42 loc) · 1.12 KB
/
example3.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
// Copyright 2014 Ardan Studios
//
// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
// From Spec:
// a short variable declaration may redeclare variables provided they
// were originally declared earlier in the same block with the same
// type, and at least one of the non-blank variables is new.
// Sample program to show some of the mechanics behind the
// short variable declaration operator redeclares.
package main
import "fmt"
// user is a struct type that declares user information.
type user struct {
id int
name string
}
func main() {
// Declare the error variable.
var err1 error
// The short variable declaration operator will
// declare u and redeclare err1.
u, err1 := getUser()
if err1 != nil {
return
}
fmt.Println(u)
// The short variable declaration operator will
// redeclare u and declare err2.
u, err2 := getUser()
if err2 != nil {
return
}
fmt.Println(u)
}
// getUser returns a pointer of type user.
func getUser() (*user, error) {
return &user{1432, "Betty"}, nil
}
// Outputs
// &{1432 Betty}
// &{1432 Betty}