You could use the spread operator on an object of type []build.TransactionMutator[]build.TransactionMutator
that is passed into the build.Transactionbuild.Transaction
method (on github.com/stellar/go/build
).
Here's a code sample:
import (
“github.com/stellar/go/build"
)
// BuildWithOps builds a transaction with a dynamic list of operations
func BuildWithOps(sourceAccount string, seq uint64, network build.Network, ops []build.TransactionMutator) (*build.TransactionBuilder, error) {
// build an array of mutators initialized with the essential values
muts := []build.TransactionMutator{
build.Sequence{Sequence: seq},
network,
build.SourceAccount{AddressOrSeed: sourceAccount},
}
// dynamically add more mutators/operations as needed
muts = append(muts, ops...)
// build the transaction from these mutators and check for an error
tx, err := build.Transaction(muts...)
if err != nil {
return nil, err
}
return tx, nil
}