惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

aimingoo的专栏
aimingoo的专栏
V
V2EX
G
Google Developers Blog
F
Full Disclosure
Martin Fowler
Martin Fowler
宝玉的分享
宝玉的分享
H
Hacker News: Front Page
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
NISL@THU
NISL@THU
G
GRAHAM CLULEY
V
Vulnerabilities – Threatpost
Hacker News - Newest:
Hacker News - Newest: "LLM"
A
About on SuperTechFans
The Cloudflare Blog
C
Cisco Blogs
D
DataBreaches.Net
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Vercel News
Vercel News
P
Privacy International News Feed
Microsoft Security Blog
Microsoft Security Blog
Help Net Security
Help Net Security
Recorded Future
Recorded Future
PCI Perspectives
PCI Perspectives
S
Schneier on Security
AI
AI
N
News | PayPal Newsroom
雷峰网
雷峰网
C
Cyber Attacks, Cyber Crime and Cyber Security
P
Proofpoint News Feed
The Last Watchdog
The Last Watchdog
L
LINUX DO - 最新话题
Hugging Face - Blog
Hugging Face - Blog
Apple Machine Learning Research
Apple Machine Learning Research
Schneier on Security
Schneier on Security
S
Securelist
云风的 BLOG
云风的 BLOG
Stack Overflow Blog
Stack Overflow Blog
博客园_首页
AWS News Blog
AWS News Blog
TaoSecurity Blog
TaoSecurity Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Recent Commits to openclaw:main
Recent Commits to openclaw:main
博客园 - 三生石上(FineUI控件)
C
CXSECURITY Database RSS Feed - CXSecurity.com
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Cloudbric
Cloudbric
C
Cybersecurity and Infrastructure Security Agency CISA
Project Zero
Project Zero
C
Check Point Blog
S
Security Affairs

博客园 - ®Geovin Du Dream Park™

go: Functional Options Pattern go:Timing Functions Pattern python: Timing Functions Pattern go: Steady-State Pattern python: Steady-State Pattern go: Handshaking Pattern python: Handshaking Pattern go: Fail-Fast Pattern python: Fail-Fast Pattern I go: Deadline Pattern python: Deadline Pattern go: Circuit-Breaker Pattern python: Circuit-Breaker Pattern go: Bulkheads Pattern python: Bulkheads Pattern go: Push & Pull Pattern python: Push & Pull Pattern python: Publish/Subscribe Pattern II go: Publish/Subscribe Pattern python: Publish/Subscribe Pattern go: Futures & Promises Pattern python: Futures & Promises Pattern go: Worker Pool Pattern go:Pipeline Pattern python: Worker Pool Pattern python: Pipeline Pattern go: Fan-Out Pattern python: Fan-Out Pattern go: Fan-In Pattern python: Fan-In Pattern Fan-In go: Producer Consumer  Pattern python: Producer Consumer Pattern go: Parallelism Pattern python: Parallelism Pattern go: Reactor Pattern python: Reactor Pattern go: Generators Pattern python: speech to text offline python: Generators Pattern go: Coroutines Pattern python:Coroutines Pattern go: Broadcast Pattern python: Broadcast Pattern python: Bounded Parallelism Pattern go: Bounded Parallelism Pattern python: N-Barrier Pattern go: N-Barrier Pattern python: Semaphore Pattern go: Semaphore Pattern python: Read-Write Lock Pattern go: Read-Write Lock Pattern python: Monitor Pattern go: Monitor Pattern python: Mutex Pattern go: Lock/Mutex Pattern Python: Condition Variable Pattern go:Condition Variable Pattern python: Registry Pattern python: Interpreter Pattern go: Registry Pattern go: Memento Pattern go: Command Pattern go: State Pattern go:Template Method Pattern go: Strategy Pattern go: Visitor Pattern go: Observer Pattern go: Mediator Pattern go: Iterator Pattern go: Chain of Responsibility Pattern go: Proxy Pattern go:Decorator Pattern go: Facade Pattern go: Flyweight Pattern go: Composite Pattern go: Singleton Pattern go: Prototype Pattern go: Bridge Pattern go: Adapter Pattern go: Builder Pattern 密码进行加盐哈希 using CSharp,Python,Go,Java go: Abstract Factory Pattern go: Model,Interface,DAL ,Factory,BLL using mysql go: Simple Factory Pattern go: Factory Method Pattern go: goLang 在Windows环境搭建Go语言开发环境 CSharp: Parallel Extensions cpp: class python: 蝴蝶跟随鼠标 javascript: 中国历史人物热力分布图using echart javascript: 中国历史人物热力图 python: 初养龙虾微信纯文字自动回复using workBuddy python: object 入门 python: Factory Method Pattern python: Simple Factory Pattern python: Abstract Factory Pattern 区域化 代码 python: Null Object Pattern python: Builder Pattern python: Adapter Pattern
go: Interpreter Pattern
®Geovin Du Dream Park™ · 2026-04-28 · via 博客园 - ®Geovin Du Dream Park™

项目结构:

image

/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述: Interpreter 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/28 21:00
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : context.go
*/
package context

type JewelryContext map[string]float64

// NewJewelryContext 创建珠宝计价上下文
func NewJewelryContext() JewelryContext {
	return make(JewelryContext)
}

// Set 设置变量
func (c JewelryContext) Set(key string, value float64) {
	c[key] = value
}

// Get 获取变量
func (c JewelryContext) Get(key string) float64 {
	return c[key]
}
/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Interpreter 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/28 21:01
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : expression.go
*/
package expression

import (
	"godesginpattern/interpreter/context"
)

// Expression 顶层表达式接口
// 所有解释器必须实现此接口
type Expression interface {
	Interpret(ctx context.JewelryContext) float64
}

// ------------------------------
// 终结符表达式(最小单元)
// ------------------------------

// Number 数字常量
type Number struct {
	value float64
}

func NewNumber(value float64) *Number {
	return &Number{value: value}
}

func (n *Number) Interpret(ctx context.JewelryContext) float64 {
	return n.value
}

// Variable 变量(金价、重量、克拉等)
type Variable struct {
	name string
}

func NewVariable(name string) *Variable {
	return &Variable{name: name}
}

func (v *Variable) Interpret(ctx context.JewelryContext) float64 {
	return ctx.Get(v.name)
}

// ------------------------------
// 非终结符表达式(运算)
// ------------------------------

// Add 加法
type Add struct {
	left  Expression
	right Expression
}

func NewAdd(left, right Expression) *Add {
	return &Add{left: left, right: right}
}

func (a *Add) Interpret(ctx context.JewelryContext) float64 {
	return a.left.Interpret(ctx) + a.right.Interpret(ctx)
}

// Sub 减法
type Sub struct {
	left  Expression
	right Expression
}

func NewSub(left, right Expression) *Sub {
	return &Sub{left: left, right: right}
}

func (s *Sub) Interpret(ctx context.JewelryContext) float64 {
	return s.left.Interpret(ctx) - s.right.Interpret(ctx)
}

// Mul 乘法
type Mul struct {
	left  Expression
	right Expression
}

func NewMul(left, right Expression) *Mul {
	return &Mul{left: left, right: right}
}

func (m *Mul) Interpret(ctx context.JewelryContext) float64 {
	return m.left.Interpret(ctx) * m.right.Interpret(ctx)
}
/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Interpreter 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/28 21:01
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : parser.go
*/
package parser

import (
	"godesginpattern/interpreter/context"
	"godesginpattern/interpreter/expression"
	"strconv"
	"strings"
)

type Parser struct {
	root expression.Expression
}

func NewParser(rule string) *Parser {
	stack := []expression.Expression{}
	tokens := strings.Fields(rule)

	for _, tok := range tokens {
		switch tok {
		case "+":
			// 正确创建:弹出两个表达式,构造Add
			right := stack[len(stack)-1]
			stack = stack[:len(stack)-1]
			left := stack[len(stack)-1]
			stack = stack[:len(stack)-1]
			stack = append(stack, expression.NewAdd(left, right))

		case "-":
			right := stack[len(stack)-1]
			stack = stack[:len(stack)-1]
			left := stack[len(stack)-1]
			stack = stack[:len(stack)-1]
			stack = append(stack, expression.NewSub(left, right))

		case "*":
			right := stack[len(stack)-1]
			stack = stack[:len(stack)-1]
			left := stack[len(stack)-1]
			stack = stack[:len(stack)-1]
			stack = append(stack, expression.NewMul(left, right))

		default:
			// 数字或变量
			if num, err := strconv.ParseFloat(tok, 64); err == nil {
				stack = append(stack, expression.NewNumber(num))
			} else {
				stack = append(stack, expression.NewVariable(tok))
			}
		}
	}

	return &Parser{root: stack[0]}
}

func (p *Parser) Calculate(ctx context.JewelryContext) float64 {
	return p.root.Interpret(ctx)
}
/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Interpreter 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/28 21:01
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : price_service.go
*/
package service

import (
	"godesginpattern/interpreter/context"
	"godesginpattern/interpreter/parser"
	"log"
)

type JewelryPriceService struct{}

func NewJewelryPriceService() *JewelryPriceService {
	return &JewelryPriceService{}
}

// 黄金价格 = 金价 × 重量 + 工费
func (j *JewelryPriceService) CalculateGoldPrice() {
	rule := "goldPrice weight * craftFee +"
	ctx := context.NewJewelryContext()
	ctx.Set("goldPrice", 600)
	ctx.Set("weight", 30)
	ctx.Set("craftFee", 500)

	price := parser.NewParser(rule).Calculate(ctx)
	log.Printf("黄金手镯:%.2f 元", price)
}

// 钻石价格 = 克拉价 × 克拉 + 金托价
func (j *JewelryPriceService) CalculateDiamondPrice() {
	rule := "diamondPrice carat * metalPrice +"
	ctx := context.NewJewelryContext()
	ctx.Set("diamondPrice", 50000)
	ctx.Set("carat", 0.8)
	ctx.Set("metalPrice", 2000)

	price := parser.NewParser(rule).Calculate(ctx)
	log.Printf("钻石戒指:%.2f 元", price)
}

// 会员折扣价 = (金价 × 重量 + 工费) × 折扣
func (j *JewelryPriceService) CalculateVIPPrice() {
	rule := "goldPrice weight * craftFee + vipDiscount *"
	ctx := context.NewJewelryContext()
	ctx.Set("goldPrice", 600)
	ctx.Set("weight", 30)
	ctx.Set("craftFee", 500)
	ctx.Set("vipDiscount", 0.9)

	price := parser.NewParser(rule).Calculate(ctx)
	log.Printf("会员折后价:%.2f 元", price)
}

调用:

/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Interpreter 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/28 21:08
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : interpreter bll.go

Interpreter /
├── cmd/
│   └── main.go
├── internal/  # 内部业务包(外部不可引用)
│   ├── context/   # 上下文环境(变量容器)
│   │   └── context.go
│   ├── expression/ # 解释器表达式核心(终结符+非终结符)
│   │   └── expr.go
│   ├── parser/ # 语法解析器
│   │   └── parser.go
│   └── service/   # 业务服务层
│       └── price_service.go
└── go.mod

*/

package bll

import (
	"godesginpattern/interpreter/service"
	"log"
)

func InterpreterMain() {
	log.Println("=== 珠宝计价系统(企业级解释器模式)===")

	srv := service.NewJewelryPriceService()
	srv.CalculateGoldPrice()
	srv.CalculateDiamondPrice()
	srv.CalculateVIPPrice()
}

输出:

image

哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)