Go基础_1_49 结构体别名

Tutorial: Go初级 Category: Go Published: 2026-04-07 13:58:26 Views: 21 Likes: 0 Comments: 0
package main

import "fmt"

type Person struct {
    name string
}

func (p Person) show() {
    fmt.Println("Person--->", p.name)
}

// 类型别名
type People = Person

func (p People) show2() {
    fmt.Println("People--->", p.name)
}

type Student struct {
    //嵌入两个结构体
    Person
    People
}

func main() {
    var s Student         //s.name = "王二狗" //ambiguous selector s.name
    s.Person.name = "王二狗" //s.show() //ambiguous selector s.show
    s.Person.show()       // Person---> 王二狗

    fmt.Printf("%T,%T\n", s.Person, s.People) //main.Person,main.Person

    s.People.name = "李小花"
    s.People.show2() // People---> 李小花
    s.Person.show()  // Person---> 王二狗
}