Struct

作者: 曹小恒 | 来源:发表于2018-10-24 00:28 被阅读0次

    We can define a object and its attributes like this:

    type person struct{
        name string
        age int
    }
    
    var P person
    P.name = "CAO_Heng"
    P.age = 25
    

    Inside the function, you can also initialize the object like this:

    P := person{"Tom",25}
    

    Embeded segment

    package main
    
    import "fmt"
    
    type Human struct{
        name string
        age int
        weight int
    }
    
    type Student struct{
        Human //Embeded segment, all elements in Human should be included here
        speciality string
    }
    

    You can also visit the embeded segments like this:

    Mark.Human = Human{"Mark",55,220}
    Maek.Human.age -= 1
    

    A comprehensive example:

    package main
    
    import "fmt"
    
    type Skill []string
    
    type Human struct{
        name string
        age int
        weight int
    }
    
    type Student struct{
        Human    //string
        Skills    //self-defined slice
        int    //build-in type as the anonymous segment
        speciality string
    }
    
    func main(){
        jane := Student{Human:Human{"Jane",35,100},speciality:"Math"}
    
        jane.Skills = []string{anatomy}
    
        jane.Skills = append(jane.Skills,"physics","golang")
    
        jane.int = 3
    }
    

    相关文章

      网友评论

          本文标题:Struct

          本文链接:https://www.haomeiwen.com/subject/vagzzftx.html