企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] ## 示例 ## Hello World <details> <summary>main.go</summary> ``` package main import "fmt" type Flow struct { name string A bool B bool C bool } type rec interface { Exec(flow *Flow) SetNext(rec) } type A struct { next rec } func (a *A) Exec(flow *Flow) { if flow.A { fmt.Printf("%+v\n", "flow A") flow.B = true } a.next.Exec(flow) } func (a *A) SetNext(r rec) { a.next = r } type B struct { next rec } func (b *B) Exec(flow *Flow) { if flow.B { fmt.Printf("%+v\n", "flow B") flow.C = true } b.next.Exec(flow) } func (b *B) SetNext(r rec) { b.next = r } type C struct { next rec } func (c *C) Exec(flow *Flow) { if flow.C { fmt.Printf("%+v\n", "ok") } } func (c *C) SetNext(r rec) { c.next = r } func main() { f := &Flow{ name: "cpj", A: true, B: false, C: false, } a := &A{} b := &B{} c := &C{} a.SetNext(b) b.SetNext(c) a.Exec(f) fmt.Printf("%+v\n", f) // output //flow A //flow B //ok // &{name:cpj A:true B:true C:true} } ``` </details> <br/> ### 医院应用 <details> <summary>main.go</summary> ``` package main import "fmt" // 处理者接口 type department interface { execute(*patient) setNext(department) } // 具体处理者 type reception struct { next department } func (r *reception) execute(p *patient) { if p.registrationDone { fmt.Println("Patient registration already done") r.next.execute(p) return } fmt.Println("Reception registering patient") p.registrationDone = true r.next.execute(p) } func (r *reception) setNext(next department) { r.next = next } type doctor struct { next department } func (d *doctor) execute(p *patient) { if p.doctorCheckUpDone { fmt.Println("Doctor checkup already done") d.next.execute(p) return } fmt.Println("Doctor checking patient") p.doctorCheckUpDone = true d.next.execute(p) } func (d *doctor) setNext(next department) { d.next = next } type medical struct { next department } func (m *medical) execute(p *patient) { if p.medicineDone { fmt.Println("Medicine already given to patient") m.next.execute(p) return } fmt.Println("Medical giving medicine to patient") p.medicineDone = true m.next.execute(p) } func (m *medical) setNext(next department) { m.next = next } type cashier struct { next department } func (c *cashier) execute(p *patient) { if p.paymentDone { fmt.Println("Payment Done") } fmt.Println("Cashier getting money from patient patient") } func (c *cashier) setNext(next department) { c.next = next } type patient struct { name string registrationDone bool doctorCheckUpDone bool medicineDone bool paymentDone bool } func main() { cashier := &cashier{} //Set next for medical department medical := &medical{} medical.setNext(cashier) //Set next for doctor department doctor := &doctor{} doctor.setNext(medical) //Set next for reception department reception := &reception{} reception.setNext(doctor) patient := &patient{name: "abc"} reception.execute(patient) } ``` </details> <br /> 输出 ``` Reception registering patient Doctor checking patient Medical giving medicine to patient Cashier getting money from patient patient ```