catalog结构的实例是通过提供实现Information接口的两个对象的值来创建的。Information类型的对象是使用Mobile类型和Shirts类型创建的。Details字段的值包含不同类型的值,所有对象的连接因子是Information接口。

package main

import(
	"fmt"
)

/*
Information接口定义了三个方法General,Attributes和Inventory
*/
type Information interface {
	General()
	Attributes()
	Inventory()
}

/*
Product结构体定义了产品属性
*/
type Product struct {
	Name, Description string
	Weight,Price float64
	Stock int
}


// Product结构体实现Information接口中的General方法func (prd Product) General() {
	fmt.Printf("\n%s",prd.Name)
	fmt.Printf("\n%s\n",prd.Description)
	fmt.Println(prd.Price)
}


// Product结构体实现Information接口中的Attributes方法func (prd Product) Attributes(){
	fmt.Println(prd.Weight)
}

// Product结构体实现Information接口中的Inventory方法func (prd Product) Inventory(){
	fmt.Println(prd.Stock)
}

// Mobile结构体中嵌入了Product结构体,因此Mobile也实现了Information接口,同时有其自定义属性type Mobile struct{
	Product
	DisplayFeatures []string
	ProcessorFeatures []string
}

// 覆盖Attributes方法func (mob Mobile) Attributes(){
	mob.Product.Attributes()
	fmt.Println("\nDisplay Features:")
	for _, key := range mob.DisplayFeatures{
		fmt.Println(key)	
	}
	fmt.Println("\nProcessor Features:")
	for _, key := range mob.ProcessorFeatures{
		fmt.Println(key)	
	}
}

// Shirts结构体也嵌入了Product结构体type Shirts struct{
	Product
	Size,Pattern,Sleeve,Fabric string
}

// 覆盖了Attributes方法func (shrt Shirts) Attributes(){
	fmt.Println("\nSpecification:")
	fmt.Println(shrt.Size)
	fmt.Println(shrt.Pattern)
	fmt.Println(shrt.Sleeve)
	fmt.Println(shrt.Fabric)
}




// Catalog有两个属性,一个是Name,一个是值为Information切片的Details
type Catalog struct{
	Name string	
	Deatails []Information
}


func (c Catalog) CatalogDetails(){

	fmt.Println("**\n")
	fmt.Printf("\nStore : %s \n",c.Name)	
	for _, key:=range c.Deatails{
		fmt.Println("===================Products==================")
		key.General()
		key.Attributes()
		key.Inventory()
		fmt.Println("=============================================")
	}
}

func main() {
	// Mobile实例mobilePrd:=Mobile{
		Product{
			"Apple iPhone 6s (Rose Gold, 32 GB)  (2 GB RAM)",
			"Handset, Apple EarPods with Remote and Mic, Lightning to USB Cable",
			40999,143,10,
		},
		[]string{"2 GB RAM","4.7 inch Retina HD Display","12MP Primary Camera"},
		[]string{"32 GB ROM","4.7 inch Retina HD Display","5MP Front"},
	}	
	
	// Shirts实例shirtPrd:=Shirts{
		Product{
			"Reebok Striped Men's Round Neck Grey T-Shirt",
			"Machine Wash as per Tag, Do not Dry Clean, Do not Bleach",
			1124,400,25,
		},
		"XL","Striped","Half Sleeve","Cotton",
	}

	// category实例category :=Catalog{
		"Amazon",		
		[]Information{mobilePrd,shirtPrd},
	}
	category.CatalogDetails()

}

输出

**


Store : Amazon
===================Products==================

Apple iPhone 6s (Rose Gold, 32 GB)  (2 GB RAM)
Handset, Apple EarPods with Remote and Mic, Lightning to USB Cable
143
40999

Display Features:
2 GB RAM
4.7 inch Retina HD Display
12MP Primary Camera

Processor Features:
32 GB ROM
4.7 inch Retina HD Display
5MP Front
10
=============================================
===================Products==================

Reebok Striped Men's Round Neck Grey T-Shirt
Machine Wash as per Tag, Do not Dry Clean, Do not Bleach
400

Specification:
XL
Striped
Half Sleeve
Cotton
25
=============================================