Go-struct嵌套初始化与赋值

struct嵌套的几种用法。

示例一

package main

import "fmt"
import "encoding/json"

type Point struct {
    X, Y int
}

type Circle struct {
    Center Point
    Radius int
}

type Wheel struct {
    Circle Circle
    Spokes int
}

func foo() {
    var w Wheel
    w.Circle.Center.X = 8
    w.Circle.Center.Y = 8
    w.Circle.Radius = 5
    w.Spokes = 20

    fmt.Println("foo(): ", w)
}

type Circle2 struct {
    Point  // anonymous fields with a named type
    Radius int
}

type Wheel2 struct {
    Circle2
    Spokes int
}

func bar() {
    var w Wheel2
    w.X = 18
    w.Y = 18
    w.Radius = 15
    w.Spokes = 120

    fmt.Println("bar(): ", w)
}

type Wheel3 struct {
    *Point
    Radius int
}

func baz() {
    var w Wheel3
    w = Wheel3{&Point{28, 28}, 25}

    json_string, err := json.Marshal(w)
    if err != nil {
        fmt.Println("Error: ", err)
    } else {
        fmt.Printf("baz(): %s\n", json_string)
    }

    fmt.Printf("baz(): %#v\n", w)

}

/*
foo():  {{{8 8} 5} 20}
bar():  {{{18 18} 15} 120}
baz(): {"X":28,"Y":28,"Radius":25}
baz(): main.Wheel3{Point:(*main.Point)(0xc04200a340), Radius:25}
*/
func main() {
    foo()
    bar()
    baz()
}

示例二

package main

import "fmt"

type Student struct {
    name string
    age  int
}

type People struct {
    Student
}

func (people *People) Print() {
    fmt.Printf("People[name:%s, age:%d]\n", people.name, people.age)
}

func PrintStudent(student *Student) {
    fmt.Printf("Student[name:%s, age:%d]\n", student.name, student.age)
}

/*
People[name:Tom, age:3]
People[name:Jerry, age:5]
Student[name:Tom, age:3]
People[name:Tom, age:4]
Student[name:Tom, age:3]
*/
func main() {
    people := People{}
    people.name = "Tom"
    people.age = 3

    people2 := People{Student{name: "Jerry", age: 5}}

    people.Print()
    people2.Print()

    var student Student
    student = people.Student
    PrintStudent(&student)

    var people3 People = People{student}
    people3.age++
    people3.Print()
    PrintStudent(&student)
}

猜你喜欢

转载自blog.csdn.net/u013344915/article/details/54428461