tensorflow/tensorflow
Go API
Go bindings for the C API. Live in tensorflow/go/.
Purpose
- Use TensorFlow from Go programs without bringing in Python or C++.
- Provide idiomatic Go wrappers around
tensorflow/c/c_api.h. - Support graph execution, eager execution, and SavedModel loading.
Directory layout
tensorflow/go/
├── README.md
├── doc.go # Package overview
├── tensor.go # Tensor type + conversions to Go slices/values
├── graph.go # Graph type, Operation type
├── session.go # Session: Run + RunWithMetadata
├── saved_model.go # SavedModel loading
├── operation.go
├── status.go
├── op/ # Generated op wrappers (genop.go produces this)
├── genop/ # Generator program (run by go generate)
└── ...How it works
The Go bindings compile via cgo against libtensorflow.so. tensor.go and friends embed // #include "tensorflow/c/c_api.h" and call C functions directly, marshalling Go values to/from C buffers.
package main
import (
"fmt"
tf "github.com/tensorflow/tensorflow/tensorflow/go"
"github.com/tensorflow/tensorflow/tensorflow/go/op"
)
func main() {
s := op.NewScope()
a := op.Const(s, [][]float32{{1, 2}, {3, 4}})
b := op.Const(s, [][]float32{{1, 0}, {0, 1}})
m := op.MatMul(s, a, b)
g, err := s.Finalize()
if err != nil { panic(err) }
sess, err := tf.NewSession(g, nil)
if err != nil { panic(err) }
defer sess.Close()
out, err := sess.Run(nil, []tf.Output{m}, nil)
if err != nil { panic(err) }
fmt.Println(out[0].Value())
}Generated op wrappers
tensorflow/go/op/wrappers.go is generated from the C++ OpDef registrations by the program in tensorflow/go/genop/. Running go generate against the Go package (after building the C library) regenerates wrappers.go.
Build / install
The Go bindings require:
- A built
libtensorflow.so(and headers) reachable fromcgo. CGO_CFLAGS=-I<path-to-include>andCGO_LDFLAGS=-L<path-to-lib> -ltensorflow.
The tensorflow/go/README.md has the canonical build instructions; in practice many Go users install pre-built libtensorflow from https://www.tensorflow.org/install/lang_c and let go get build the wrappers.
Stability
The Go API piggybacks on the stable C API, so it inherits the same backwards-compatibility guarantees. The op wrappers in tensorflow/go/op/wrappers.go are regenerated against each release, so signatures may pick up new attributes.
Integration points
- Backed by:
tensorflow/c/c_api.handtensorflow/c/eager/c_api.h. - Used by: Go-based serving systems, TFX-style services, and standalone CLI tools that need to load SavedModels.
Entry points for modification
- New idiomatic helper — add to
tensorflow/go/. - Updates to wrappers — change happens automatically when an
OpDefchanges; runninggo generateupdateswrappers.go.
Related
- c-api — the underlying ABI.
- features/saved-model — SavedModel format used by
tf.SavedModelin Go.
Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.