-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnameUnnamed.go
53 lines (45 loc) · 1.08 KB
/
nameUnnamed.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
// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
// Sample program to show how variables of an unnamed type can
// be assigned to variables of a named type, when they are
// identical.
package main
import "fmt"
// example represents a type with different fields.
type example struct {
flag bool
counter int16
pi float32
}
type example1 struct {
flag bool
counter int16
pi float32
}
func main() {
// Declare a variable of an anonymous type and init
// using a struct literal.
e := struct {
flag bool
counter int16
pi float32
}{
flag: true,
counter: 10,
pi: 3.141592,
}
// Create a value of type example.
var ex example
var ex1 example1
// Assign the value of the unnamed struct type
// to the named struct type value.
ex = e
ex1 = e
// Display the values.
fmt.Printf("%+v\n", ex)
fmt.Printf("%+v\n", e)
fmt.Println("Flag", e.flag)
fmt.Println("Counter", e.counter)
fmt.Println("Pi", e.pi)
fmt.Println(ex1)
}