示例

// You can edit this code!
// Click here and start typing.
package main

import "fmt"

type BaseModel struct {
    ID int64
}

func (b BaseModel) GetID() int64 {
    return b.ID
}

type Product struct {
    BaseModel
    Title string
    Price float64
}

type Factory struct {
    BaseModel
    Addr, Name string
}

type GetIDer interface {
    GetID() int64
}

func doSomethingOnlyNeedID(m GetIDer) {
    fmt.Println("--TODO--inFunc--m.GetID()=", m.GetID())
}

func main() {
    prod := Product{Title: "品牌OEM羽毛球运动鞋男女同款", Price: 299.99}
    // 对嵌套结构体赋值时,必须用 = 符号,对每个属性单独赋值
    prod.ID = 123456789

    factory := Factory{Name: "泉州幸福鞋业制造厂", Addr: "福建省泉州市幸福工业区33号"}
    // 使用 = 符号,对嵌套结构体的属性赋值
    factory.ID = 121212

    fmt.Println("---outFunc-prod.GetID()=", prod.GetID(), "----factory.GetID()=", factory.GetID())
    doSomethingOnlyNeedID(prod)
    doSomethingOnlyNeedID(factory)
}

输出:

---outFunc-prod.GetID()= 123456789 ----factory.GetID()= 121212
--TODO--inFunc--m.GetID()= 123456789
--TODO--inFunc--m.GetID()= 121212