11- structure

Structure

What is the structure

Type structure is a user-defined, represents a set of several fields (Field) is. Sometimes the data should be integrated, instead of letting these data is not linked. In this case the structure may be used. A collection of attributes

The definition of the structure

name structure type struct {}
// definition of the structure of one 
type struct {the Person 
      name String 
      Sex int 
      Age int // if it is the same type can be written as: Sex, Age int 
}

Anonymous structure ( no type and name )

main Package
 Import  " FMT " 

FUNC main () {
 // create anonymous structure assignment and
 A: = struct { 
    name String 
    Age int 
} { "AAA", 20 is } // Assignment 
fmt.Println (A) 

} 
# results 
{aaa 20}

Create a structure and assign

main Package
 Import  " FMT " 
// define person structure
 type the Person struct { 
    name String 
    Age, Sex int 
} 
FUNC main () { 
  # a first embodiment assignment , according to key, value form PERSON1:
= the Person { name: " Bob " , Age: 10 , Sex: . 1 , }
  # the second assignment, the location parameter passing PERSON2:
= {the Person " JJ " , 20 is , 0}
fmt.Println (PERSON1) fmt.Println (PERSON2) }
# results {bob 10 1} {jj 20 0}

Structure value of zero (0 is printed, indicating that the structure is a value type )

main Package
 Import  " FMT "
 
// define person structure 
type struct {the Person 
    name String 
    Age, Sex int 
} 

FUNC main () { var person the Person
       // also write person: = the Person {} 
  fmt.Println (person) 
} 
# results 
{00}

Field access structure

Dot operator  A field for access structure

package main
import (  
    "fmt"
)

type Employee struct {  
    firstName, lastName string
    age, salary         int
}

func main() {  
    emp6 := Employee{"Sam", "Anderson", 55, 6000}  //结构体赋值
    fmt.Println("First Name:", emp6.firstName)
    fmt.Println("Last Name:", emp6.lastName)
    fmt.Println("Age:", emp6.age)
    fmt.Printf("Salary: $%d", emp6.salary)
}

Pointer structure

You can also create a pointer to a structure

main Package
 Import  " FMT "
 
// define person structure 
type struct {the Person 
    name String 
    Age, Sex int 
} 
FUNC main () { 
  # define different kinds of ways PERSON2:
     = {& the Person "JJ", 20,0} 
    var = & person3 the Person { "XX", 30,1 } 
    fmt.Println ( ( * PERSON2) .name ) // JJ 
    fmt.Println (( * person3) .name) // XX 
    fmt.Println ( person2.name )          // JJ 
    FMT. println (person3.name)          // XX 
}

Go language allows us the time to access the name field, you can use instead of person2.name (* person2) .name, write it more convenient and simple.

Anonymous field

When we create a structure, the field can only type without field names. This field is called an anonymous field.

The following code creates a  Person structure that contains two anonymous field  string and  int.

type Person struct {  
    string
    int
}
package main
import (  
    "fmt"
)
type Person struct {  
    string
    int
}

func main() {  
    p := Person{"Naveen", 50}
    fmt.Println(p)
}
#结果
{Naveen 50}

Although anonymous field has no name, but in fact the name of the anonymous field will default to its type . For example, in the above Person structure, the field although anonymous, but Go default these field names are their respective types. Therefore, Person the structure has two named string and int fields.

main Package
 Import (  
     " FMT " 
) 
type the Person struct {   
    String 
    int 
} # anonymous functions may be assigned according to their type 
FUNC main () {   
    var P1 the Person p1.string

     = "Naveen" 
    p1.int = 50 
    fmt.Println ( P1) 
}

Nesting structure

Field structure there may also be a structure. Such a structure is called a nested structure.

main Package
 Import (  
     " FMT " 
) 

type the Address struct {   
    City, State String 
} 
type struct {the Person   
    name String 
    Age int address the Address # nested structure: a structure field name + 
} 
FUNC main () {   
    var P the Person 
    p.name
       
 = " Naveen " 
    p.age = 50 
    p.address = the Address { 
        City: " Chicago " , 
        State: " Illinois " , 
    } 
    fmt.Println ("Name:", p.name)
    fmt.Println("Age:",p.age)
    fmt.Println("City:",p.address.city)
    fmt.Println("State:",p.address.state)
}
// nested structure of another embodiment assignment
 P: = {the Person name: "LQZ", Hobby: Hobby {10, "basketball"}} 
P: = {the Person name: "LQZ", Hobby: Hobby {ID: 10, name: "basketball" }} 
p.hobby.id = 101

Lifting field (+ AnonymousField nested structure) (******)

// + nested structure AnonymousField 
type struct {the Person 
    name String 
    Sex, Age int Hobby 
} 
type struct {Hobby 
    ID int hobbyname String 
} 
FUNC main () {
    
   

 // structure nest anonymous field plus 
    P: = {the Person name : " LQZ " , Hobby: Hobby {10, " basketball " }}
     // get the p hobbyname, anonymous function can not get to the body structure by a direct property value, the property name does not have the same name and in the nested structure can this 
    fmt.Println (p.Hobby.hobbyname) 
    fmt.Println (the p-. hobbyname ) 
} 
# results 
basketball 
basketball

The following are the properties of the same name, the variable will not be improved (similar object-oriented inheritance)

// + nested structure AnonymousField 
type struct {the Person 
    name String 
    Sex, Age int Hobby 
} 
type struct {Hobby 
    ID int name String 
} 
FUNC main () {
    
   

 // structure nest anonymous field plus 
    P: = {the Person name : " LQZ " , Hobby: Hobby {10, " basketball " }}
     // get the p name, attribute name and nested structure has the same name attribute, if the direct use of this method will find p.name the name attribute of the structure, will not improve 
    fmt.Println (p.Hobby.name) 
    fmt.Println (p.name) 
} 
# results 
basketball 
lqz

Equality structure

It is a value type structure. If each of its fields are comparable, the structure is comparable. If the two structures corresponding field variables are equal, then the two variables are equal.

package main
import (  
    "fmt"
)

type name struct {  
    firstName string
    lastName string
}


func main() {  
    name1 := name{"Steve", "Jobs"}
    name2 := name{"Steve", "Jobs"}
    if name1 == name2 {
        fmt.Println("name1 and name2 are equal")
    } else {
        fmt.Println("name1 and name2 are not equal")
    }

    name3 := name{firstName:"Steve", lastName:"Jobs"}
    name4 := name{}
    name4.firstName = "Steve"
    if name3 == name4 {
        fmt.Println("name3 and name4 are equal")
    } else {
        fmt.Println("name3 and name4 are not equal")
    }
}

 

Guess you like

Origin www.cnblogs.com/wangcuican/p/12030934.html