/
structs_Declare_Initialize.go
57 lines (50 loc) · 1.57 KB
/
structs_Declare_Initialize.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
package main
import (
"fmt"
)
type Address struct {
name string
street string
city string
state string
Pincode int
}
func main() {
type User struct{ Name string }
type User1 User // new type User1
type User2 = User // User2 is an alias of User
user0 := User{Name: "user0"}
user1 := User1{Name: "user1"}
user2 := User2{Name: "user2"}
fmt.Printf("user0 type: %T, value: %v\n", user0, user0)
fmt.Printf("user1 type: %T, value: %v\n", user1, user1)
fmt.Printf("user2 type: %T, value: %v\n", user2, user2)
// Defining Name type
type Shop struct {
Id string `json:"id"`
Name string `json:"name"`
Owner string `json:"owner"`
Order []string
Address Address `json:"address"`
}
// Defining Anonymous type
shopAnonymous := struct {
Id string `json:"id"`
Name string `json:"name"`
Owner string `json:"owner"`
}{Id: "1", Name: "Anonymous Type"}
fmt.Printf("shopAnonymous: %T, [%v] \n", shopAnonymous, shopAnonymous)
// initializing a struct with zero value
shop0 := Shop{}
fmt.Println("initializing a struct with zero value: ", shop0)
// initializing a struct with name field and access fields of a struct
shop1 := Shop{Name: "n1", Id: "b"}
fmt.Println("initializing a struct with name field: ", shop1)
fmt.Println("access fields of a struct", shop1.Name)
// initializing a struct with not name field
shop2 := Shop{"1", "n", "", []string{}, Address{}}
fmt.Println("initializing a struct with not name field: ", shop2)
// new a struct type with zero value
shop3 := new(Shop)
fmt.Println("new a struct type with zero value: ", shop3)
}