Go bindings for the AMPL API, built on the AMPL C API (libampl) via cgo.
- Go 1.20 or newer, with a C compiler available for cgo
- An AMPL installation that includes the C API (
amplapi/includeandamplapi/lib) and a valid license
go get github.com/ampl/amplgoThe C API is not bundled with this module. Tell cgo where to find it, and make the shared library
and the ampl executable available at run time:
export AMPL_DIR=/path/to/ampl.linux-intel64
export CGO_CFLAGS="-I$AMPL_DIR/amplapi/include"
export CGO_LDFLAGS="-L$AMPL_DIR/amplapi/lib"
export LD_LIBRARY_PATH="$AMPL_DIR/amplapi/lib:$LD_LIBRARY_PATH" # DYLD_LIBRARY_PATH on macOS
export PATH="$AMPL_DIR:$PATH"package main
import (
"fmt"
"github.com/ampl/amplgo/ampl"
)
func main() {
model, err := ampl.New()
if err != nil {
panic(err)
}
if err := model.Read("models/diet/diet.mod"); err != nil {
panic(err)
}
if err := model.ReadData("models/diet/diet.dat"); err != nil {
panic(err)
}
if err := model.Solve("", "highs"); err != nil {
panic(err)
}
variant, err := model.GetValue("Total_Cost")
if err != nil {
panic(err)
}
totalCost, _ := variant.GetNumericValue()
fmt.Println("Total Cost:", totalCost)
}More examples are in examples/. Run them from the repository root so the relative model paths resolve:
go run ./examples/quickstart
go run ./examples/firstexample highsdev/updatelib.py downloads the C API headers and libraries into ampl/include and ampl/lib
(both git-ignored). Point the cgo variables there to build against them:
python dev/updatelib.py
export CGO_CFLAGS="-I$PWD/ampl/include"
export CGO_LDFLAGS="-L$PWD/ampl/lib/amd64"
export LD_LIBRARY_PATH="$PWD/ampl/lib/amd64:$LD_LIBRARY_PATH"
go test ./testsMIT, see LICENSE. The AMPL C API and the AMPL executable are distributed separately under their own license terms.