examples/go/README.md
This directory contains examples demonstrating how to use the Z3 Go bindings.
Demonstrates fundamental Z3 operations:
# Build Z3 first
cd ../..
mkdir build && cd build
cmake ..
make -j$(nproc)
# Set up environment
cd ../examples/go
export LD_LIBRARY_PATH=../../build:$LD_LIBRARY_PATH
export CGO_CFLAGS="-I../../src/api"
export CGO_LDFLAGS="-L../../build -lz3"
# Run examples
go run basic_example.go
REM Build Z3 first
cd ..\..
mkdir build
cd build
cmake ..
cmake --build . --config Release
REM Set up environment
cd ..\examples\go
set PATH=..\..\build\Release;%PATH%
set CGO_CFLAGS=-I..\..\src\api
set CGO_LDFLAGS=-L..\..\build\Release -lz3
REM Run examples
go run basic_example.go
When you run basic_example.go, you should see output similar to:
Z3 Go Bindings - Basic Example
================================
Example 1: Solving x > 0
Satisfiable!
Model: ...
x = 1
Example 2: Solving x + y = 10 ∧ x - y = 2
Satisfiable!
x = 6
y = 4
Example 3: Boolean satisfiability
Satisfiable!
p = false
q = true
Example 4: Checking unsatisfiability
Status: unsat
The constraints are unsatisfiable (as expected)
All examples completed successfully!
Import the Z3 package:
import "github.com/Z3Prover/z3/src/api/go"
Create a context:
ctx := z3.NewContext()
Create variables and constraints:
x := ctx.MkIntConst("x")
constraint := ctx.MkGt(x, ctx.MkInt(0, ctx.MkIntSort()))
Solve:
solver := ctx.NewSolver()
solver.Assert(constraint)
if solver.Check() == z3.Satisfiable {
model := solver.Model()
// Use model...
}
Make sure:
Make sure:
Ensure:
See the README.md in src/api/go for complete API documentation.