十.Go结构struct

2017-06-21  本文已影响0人  kaxi4it

结构struct

package main
import (
    "fmt"
)
type animal struct {
    Name string
    Age  int
}
type cat struct {
    animal
    Legs  int
    Sound string
}
type dog struct {
    animal
    Age int
}
type unknow struct {
    Name    string
    Details struct {
        Age, Country string
    }
}
func main() {
    c1 := &cat{Legs: 4, Sound: "喵", animal: animal{Name: "Tom", Age: 2}}
    fmt.Println(c1)
    modifyCat(c1)
    fmt.Println(c1)
    c2 := &struct {
        Name string
        Age  int
    }{
        Name: "王大锤",
        Age:  22,
    }
    fmt.Println(c2)
    u1 := &unknow{Name: "外星人"}
    u1.Details.Age = "111"
    u1.Details.Country = "火星"
    fmt.Println(u1)
    d1 := &dog{}
    d1.animal.Name = "狗狗"
    d1.Age = 6
    d1.animal.Age = 10
    fmt.Println(d1)
    d2 := &dog{animal: animal{Name: "狗狗Tom", Age: 12}, Age: 9}
    fmt.Println(d2)
}
func modifyCat(mCat *cat) {
    mCat.Name = "Jerry"
    mCat.Age = 1
    mCat.Legs = 5
    mCat.Sound = "喵喵"
}

直通车

一.Go开发工具及命令
二.Go编程基础知识
三.Go的类型与变量
四.Go常量与运算符
五.Go控制语句
六.Go数组
七.Go切片slice
八.Go哈希字典map
九.Go函数func
十.Go结构struct

上一篇下一篇

猜你喜欢

热点阅读