We want to implement our business logic using Go, but we cannot find any good implementation of rules engine/inference engine for Go. Does anyone have any experience or suggestions?
-
Did you find any good soliton or framework around this topic? @rui – Mikheil Janiashvili Sep 01 '23 at 17:26
4 Answers
There is a project that aims to implement an ISO Prolog compiler in Go:
I haven't tested it, but given that it implements some basic Prolog, that should be quite a capable rule-based reasoning engine, AFAIS.
Otherwise, a search for "rule" over at godoc.org also yields a bunch of packages:

- 4,336
- 5
- 42
- 63
-
Looking at the list over at godoc.org, godec (https://github.com/steveyen/gdec) seems to be potentially interesting , claiming to be "declarative golang data & rules processing". – Samuel Lampa May 08 '14 at 21:18
The best example of something like this to my knowledge is the 'table-driven' approach to unit tests taken in much of the standard library. For example, the fmttests.
Beyond that, Go is a powerful, expressive language. What do you actually need? There are a number of examples of state machine implementations in Go, and a number of web frameworks with declarative JSON configuration.
If you mean proper logic programming, there's no popular Go library for it yet.

- 1,879
- 2
- 20
- 29
-
It is not for testing purpose, we just need a temptable framework to handle boolean evaluation from a rule description file instead of hard code the logic in the code. – rui Jun 26 '13 at 22:16
If you're familiar with JBoss Drools, now there's something similar in Golang. Check this out https://github.com/newm4n/grool
It has DSL similar to Drools DRL, called GRL.
rule SlowDown "When testcar is slowing down we keep decreasing the speed." salience 10 {
when
TestCar.SpeedUp == false && TestCar.Speed > 0
then
TestCar.Speed = TestCar.Speed - TestCar.SpeedIncrement;
DistanceRecord.TotalDistance = DistanceRecord.TotalDistance + TestCar.Speed;
}

- 680
- 3
- 6
-
How to write the rule for "Contains" operator in GRL ? Rest I went through your grool repo and liked what you have done. – Kartavya Ramnani Nov 24 '19 at 10:13
-
@Kartavya You can easily create any function inside a struct and add the struct instance into the data context. Call the function from GRL. – Ferdinand Neman Dec 24 '19 at 22:27
-
1This project is now archived. Moved to https://github.com/hyperjumptech/grule-rule-engine – Ferdinand Neman Jul 16 '20 at 05:03
Take a look at https://github.com/antonmedv/expr
It can parse next expressions:
# Get the special price if
user.Group in ["good_customers", "collaborator"]
# Promote article to the homepage when
len(article.Comments) > 100 and article.Category not in ["misc"]
# Send an alert when
product.Stock < 15
Type check them and evaluate.

- 3,393
- 3
- 28
- 40