Go-struct

Structure: Aggregated data type that combines 0 or more named variables of any type.

Such as:

type Employee struct{

    ID int
    Name string
    Address string
    DoB time.Time
    Position string
    Salary int
    ManagerId int
}

var dilbert Employee

//成员访问
dilbert.Salary -= 5000

//获取成员变量的地址,然后通过指针访问
position := &dilbert.Position
*position = "Senior" + *position

//点号作用在结构体指针上
var employeeOfTheMonth *Employee = &dilbert
employeeOfTheMonth.Position += "(proctive team player)"

The aggregation type S cannot contain itself, but a pointer type of S can be defined in S, that is, * S, and some recursive data types such as linked lists and trees can be created.

type tree struct{
    value int
    left,right *tree
}


func add(t *tree, value int) *tree{

    if t == nil{
        //等价于返回&tree{value,value}
        t = new(tree)
        t.value = value
        return t
    }

    if value < t.value{
        t.left = add(t,left, value)
    }else{  
        t.right = add(t.right, value)
    }
}


//将元素按顺序追加到values里面,然后返回slice

func appendValues(values []int, t *tree)[]int{

    if t != nil{
        values = appendValues(values, t.left)
        values = append(values, t.vallue)
        values = appendValues(values, t.right)
    }

    return values
}

Two formats of structure literals

(1) type Point struct {X, Y int} P: = Point {1,2} // in the correct order of member variables

(2) By specifying the names of some or all member variables, if a member variable is not specified, its value is the zero value of the member variable

   P := Point{X:1}

If the structure does not have any members, it is an empty structure , write struct {} . Its size is 0, and it does not contain any information, but sometimes it is still valuable. Some Go language programmers use map to simulate the set data structure, and use it to replace the Boolean value in the map, only to emphasize the importance of key, but because of the limited space savings and the more complicated syntax, we usually avoid avoiding such usage.

seen := make(map[string]struct{}) // set of strings
// ...
if _, ok := seen[s]; !ok {
    seen[s] = struct{}{}
    // ...first time seeing s...
}

Structures can be used as function parameters and return values. If the structure is large, generally use pointer parameters, and if you want to modify the structure in the function, you must use the pointer form. All function parameters in go language are value copies.

func Bonus( e *Employee, percent int) int{
    return e.Salary * percent / 100
}

func AwardAnnualRaise (e *Employee){ //接收的是实参的副本
    e.Salary = e.Salary * 105 / 100
}

pp := &Point{1, 2} <==> pp := new{Point} *pp = Point{1,2}

If all members of the structure are comparable, the structure is also comparable, and can be used as the key type of Map. More practical == or! =

 

 

 

 

 

 

 

 

 

 

 

Published 127 original articles · Likes 24 · Visits 130,000+

Guess you like

Origin blog.csdn.net/Linzhongyilisha/article/details/99630304