table of Contents
table of Contents
@
Definition and use
- Go in the C struct and struct very similar and Go no class, no constructor
- Use
type <name> strct{}
structure definition, names follow visibility rules (start with a capital represents the public, begins with a non-capital as private) Pointer type pointing to itself members (similar to this ??)
- You can use literal to initialize structure
Allow structure members directly read by a pointer (a '')
type person struct{
Name string
Age int
}
func main(){
//go语言习惯上定义的时候用取地址符,这样作为指针使用会很方便,而且是不是指针都可以直接用'.'来取值
a := &person{}
a.Name = "joe"
a.Age = 19
/*字面值对结构进行初始化
a := &person{
Name: "joe",
Age: 19,
}
*/
a.Name = "ok"
fmt.Println(a)
A(a)
fmt.Println(a)
B(a)
fmt.Println(a)
}
func A(per person){
//值拷贝
per.Age = 13
fmt.Println("A",per)
}
func B(per *person){
//引用拷贝
per.Age = 13
fmt.Println("A",per)
}
/*
> Output:
{joe 19}
A {joe 13}
{joe 19}
A &{joe 13}
{joe 13}
*/
Anonymous structure
- Anonymous support structure, can be used as a member or member variables defined
- Anonymous structure can also be used to map the value of
func main(){
//匿名结构
a := &struct {
Name string
Age int
}{
Name : "joe",
Age : 19,
}
fmt.Println(a)
}
type person struct {
Name string
Age int
Contact struct{
Phone, City string
}
}
func main(){
a := person{Name:"joe", Age: 19}
a.Contact.Phone = "1234565875"
a.Contact.City = "beijing"
fmt.Println(a)
}
Anonymous field
- Support anonymous field, essentially defines a field named name to a type
- Embedded structure as an anonymous field looks like inheritance, but not inherited
- You can use an anonymous field pointer
type person struct {
string
int
}
func main(){
//字段顺序一定要一致
a := person{"joe", 19}
b := a
fmt.Println(a)
fmt.Println(b)
}
Assignment and comparison between the structure
- Members of the same type can directly copy assignment
- And support ==! = Comparison operators, comparison between only the same type, but not> or <
type person struct {
Name string
Age int
}
type person1 struct {
Name string
Age int
}
func main(){
a := person{Name:"joe", Age:19}
b := person1{Name:"joe", Age:19}
fmt.Println(a)
fmt.Println(b)
fmt.Println( b == a)
}
/*
> Output:
command-line-arguments
# command-line-arguments
.\hello2.go:18:15: invalid operation: b == a (**mismatched types person1 and person)**
*/
Embedded structure
- Go language class and there is no inheritance
- Embedded structure name as the type name on
type human struct{
Sex int
}
type teacher struct {
human
Name string
Age int
}
type student struct{
human
Name string
Age int
}
func main(){
a := teacher{Name:"joe", Age:19,human: human{Sex:0}}
b := student{Name:"joe", Age:19,human: human{Sex:1}}
a.Name ="joe2"
a.Age = 13
a.Sex =100
fmt.Println(a, b)
}
/*
> Output:
command-line-arguments
{{100} joe2 13} {{1} joe 19}
*/
When using anonymous fields or embedded fields, if the inner and outer structure has the same field name, how to do it?
type A struct {
B
C
Name string
}
type B struct {
Name string
}
type C struct {
Name string
}
func main(){
a := A{Name:"A",B: B{Name:"B"},C: C{Name:"C"}}
fmt.Println(a.Name, a.B.Name,a.C.Name)
}
/*
> Output:
command-line-arguments
A B C
*/
Not found in the current hierarchy, then it is a deeper structure to find
pictures, C if A structure on the inside, then it will be ambiguous a.Name