项目结构/* # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看言語成了邀功盡責的功臣還需要行爲每日來值班嗎 # 描述Visitor Pattern 访问者模式 # Author : geovindu,Geovin Du 涂聚文. # IDE : goLang 2024.3.6 go 26.2 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/4/23 20:16 # User : geovindu # Product : GoLand # Project : godesginpattern # File : jewelry.go */ package domain // Jewel 珠宝接口 // 这里只定义 Accept不引用任何 Visitor 接口 type Jewel interface { Accept(v any) } // Diamond 钻石 type Diamond struct { Carat float64 Clarity string PricePerCarat float64 } func (d *Diamond) Accept(v any) { // 由 visitor 内部断言处理 } // Gold 黄金 type Gold struct { Weight float64 Purity string PricePerGram float64 } func (g *Gold) Accept(v any) {} // Jade 翡翠 type Jade struct { Color string Texture string BasePrice float64 } func (j *Jade) Accept(v any) {}/* # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看言語成了邀功盡責的功臣還需要行爲每日來值班嗎 # 描述Visitor Pattern 访问者模式 # Author : geovindu,Geovin Du 涂聚文. # IDE : goLang 2024.3.6 go 26.2 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/4/23 20:17 # User : geovindu # Product : GoLand # Project : godesginpattern # File : cleaner.go */ package visitor import ( fmt godesginpattern/visitordesgin/domain ) // Cleaner 清洁师 type Cleaner struct{} func NewCleaner() *Cleaner { return Cleaner{} } func (c *Cleaner) VisitDiamond(d *domain.Diamond) { fmt.Println([清洁] 钻石超声波深度去污) } func (c *Cleaner) VisitGold(g *domain.Gold) { fmt.Println([清洁] 黄金酸洗抛光提亮) } func (c *Cleaner) VisitJade(j *domain.Jade) { fmt.Println([清洁] 翡翠软布温水养护) } /* # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看言語成了邀功盡責的功臣還需要行爲每日來值班嗎 # 描述Visitor Pattern 访问者模式 # Author : geovindu,Geovin Du 涂聚文. # IDE : goLang 2024.3.6 go 26.2 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/4/23 20:17 # User : geovindu # Product : GoLand # Project : godesginpattern # File : identifier.go */ package visitor import ( fmt godesginpattern/visitordesgin/domain ) // Identifier 鉴定师 type Identifier struct{} func NewIdentifier() *Identifier { return Identifier{} } func (i *Identifier) VisitDiamond(d *domain.Diamond) { fmt.Printf([鉴定] 钻石净度 %s - 优质天然钻\n, d.Clarity) } func (i *Identifier) VisitGold(g *domain.Gold) { fmt.Printf([鉴定] 黄金纯度 %s - 高纯度足金\n, g.Purity) } func (i *Identifier) VisitJade(j *domain.Jade) { fmt.Printf([鉴定] 翡翠 %s/%s - 天然A货\n, j.Color, j.Texture) } /* # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看言語成了邀功盡責的功臣還需要行爲每日來值班嗎 # 描述Visitor Pattern 访问者模式 # Author : geovindu,Geovin Du 涂聚文. # IDE : goLang 2024.3.6 go 26.2 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/4/23 20:17 # User : geovindu # Product : GoLand # Project : godesginpattern # File : iface.go */ package visitor import godesginpattern/visitordesgin/domain // Visitor 接口 放在 visitor 包内 // 单向依赖 domain无循环 type Visitor interface { VisitDiamond(d *domain.Diamond) VisitGold(g *domain.Gold) VisitJade(j *domain.Jade) } /* # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看言語成了邀功盡責的功臣還需要行爲每日來值班嗎 # 描述Visitor Pattern 访问者模式 # Author : geovindu,Geovin Du 涂聚文. # IDE : goLang 2024.3.6 go 26.2 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/4/23 20:17 # User : geovindu # Product : GoLand # Project : godesginpattern # File : valuator.go */ package visitor import ( fmt godesginpattern/visitordesgin/domain ) // Valuator 估值师 type Valuator struct{} func NewValuator() *Valuator { return Valuator{} } // VisitDiamond 钻石估值 func (v *Valuator) VisitDiamond(d *domain.Diamond) { total : d.Carat * d.PricePerCarat fmt.Printf([估值] 钻石 %.2f克拉 | 总价: %.2f\n, d.Carat, total) } // VisitGold 黄金估值 func (v *Valuator) VisitGold(g *domain.Gold) { total : g.Weight * g.PricePerGram fmt.Printf([估值] 黄金 %.2fg | 总价: %.2f\n, g.Weight, total) } // VisitJade 翡翠估值 func (v *Valuator) VisitJade(j *domain.Jade) { fmt.Printf([估值] 翡翠 %s/%s | 评估价: %.2f\n, j.Color, j.Texture, j.BasePrice) }/* # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看言語成了邀功盡責的功臣還需要行爲每日來值班嗎 # 描述Visitor Pattern 访问者模式 # Author : geovindu,Geovin Du 涂聚文. # IDE : goLang 2024.3.6 go 26.2 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/4/23 20:17 # User : geovindu # Product : GoLand # Project : godesginpattern # File : collection.go */ package structure import ( godesginpattern/visitordesgin/domain godesginpattern/visitordesgin/visitor ) // JewelCollection 珠宝集合 type JewelCollection struct { jewels []domain.Jewel } func NewJewelCollection() *JewelCollection { return JewelCollection{ jewels: make([]domain.Jewel, 0), } } // Add 添加珠宝 func (c *JewelCollection) Add(j domain.Jewel) { c.jewels append(c.jewels, j) } // Accept 访问者处理所有珠宝 func (c *JewelCollection) Accept(v visitor.Visitor) { for _, item : range c.jewels { switch j : item.(type) { case *domain.Diamond: v.VisitDiamond(j) case *domain.Gold: v.VisitGold(j) case *domain.Jade: v.VisitJade(j) } } }调用/* # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看言語成了邀功盡責的功臣還需要行爲每日來值班嗎 # 描述 # Author : geovindu,Geovin Du 涂聚文. # IDE : goLang 2024.3.6 go 26.2 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/4/23 20:36 # User : geovindu # Product : GoLand # Project : godesginpattern # File : visitorbll.go */ package bll import ( fmt godesginpattern/visitordesgin/domain godesginpattern/visitordesgin/structure godesginpattern/visitordesgin/visitor ) func VisitorMain() { // 1. 创建珠宝 diamond : domain.Diamond{Carat: 1.5, Clarity: VVS1, PricePerCarat: 80000} gold : domain.Gold{Weight: 20, Purity: 9999, PricePerGram: 600} jade : domain.Jade{Color: 帝王绿, Texture: 玻璃种, BasePrice: 180000} // 2. 创建集合 coll : structure.NewJewelCollection() coll.Add(diamond) coll.Add(gold) coll.Add(jade) // 3. 执行访问者统一用 fmt 保证输出顺序正确 fmt.Println( 估值师 ) coll.Accept(visitor.NewValuator()) fmt.Println(\n 鉴定师 ) coll.Accept(visitor.NewIdentifier()) fmt.Println(\n 清洁师 ) coll.Accept(visitor.NewCleaner()) }输出