17 Golang结构体详解(三)

learninginto · · 537 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

一、结构体嵌套
  • demo1
type User struct {
    username string
    password string 
    sex string
    age int 
    address Address //User结构体嵌套Address结构体
}

type Address struct {
    name string
    phone string
    city string
}

func main() {
    var u User
    u.username = "lxc"
    u.password = "123456"
    u.address.name = "张三"
    u.address.phone = "010-1234567"
    u.address.city = "北京"
    fmt.Printf("%#v",u)
    //main.User{username:"lxc", password:"123456", sex:"", age:0, address:main.Address{name:"张三", phone:"010-1234567", city:"北京"}}
}
  • 嵌套匿名结构体
type User stuct {
    username string
    password string 
    Address
}

type Address struct {
    name string
    phone string
    city string
}

func main() {
    var u User
    u.city = "上海"
    fmt.Println(u.city)//上海
    fmt.Println(u.Address.city)//上海
}
  • 关于嵌套结构体的字段名冲突(就近原则)
type User struct {
    username string 
    password string
    addtime string
    Address
}

type Address struct {
    name string
    phone string
    city string
    addtime string
}

type Email struct {
    account string
    addtime string
}

func main () {
    var u User
    u.city = "上海"
    u.addtime = "2021-03-10"
    fmt.Println(u)
    //main.User{username:"", password:"", addtime:"2021-03-10", Address:main.Address{name:"",phone:"", city:"上海", addtime:""}}
    u.Address.addtime = "2021-06-10"
    fmt.Println(u)
}
  • 当两个子结构同级、就近原则不法判断时,程序就会报错
type User struct {
    username string
    password string
    Address
    Email
}

type Address struct {
    name string 
    phone string
    city string
    addtime string
}

type Email struct {
    account string
    addtime string
}

func main() {
    var u User
    //u.addtime = "2021-06-06"//报错 ambiguous selector u.addtime
    u.Address.addtime = "2021-06-06"
    u.Email.addtime = "2021-06-07"
    fmt.Println(u)
    //main.User{username:"", password:"", Address:main.Address{name:"", phone:"", city:"", addtime:"2021-06-06"}, Email:main.Email{account:"", addtime:"2021-06-07"}}
}
二、结构体的继承

Go语言中,结构体的继承是通过嵌套实现的

每次都从子结构体向父结构体中去找

//父结构体
type Animal struct {
    Name string
}

//子结构体
type Dog struct {
    Age int
    Animal //结构体嵌套
}

func (a Animal) run() {
    fmt.Printf("%v在运动 ", a.Name)
}

func (d Dog) wang() {
    fmt.Printf("%v在汪汪 ", d.Name)
}

func main() {
    var d = Dog {
        Age : 20,
        Animal : Animal{
            Name : "旺财",
        },
    }
    d.run()
    d.wang()
    //旺财在运动 旺财在汪汪 
}
  • 通过传递指针
type Animal struct {
    Name string
}

type Dog struct {
    Age int
    *Animal //结构体嵌套
}

func (a Animal) run() {
    fmt.Printf("%v在运动 ", a.Name)
}

func (d Dog) wang() {
    fmt.Printf("%v在汪汪 ", d.Name)
}

func main() {
    var d = Dog {
        Age : 20,
        Animal : &Animal{
            Name : "旺财",
        },
    }
    d.run()
    d.wang()
    //旺财在运动 旺财在汪汪 
}

有疑问加站长微信联系(非本文作者)

本文来自:简书

感谢作者:learninginto

查看原文:17 Golang结构体详解(三)

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

537 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传