rdf-go Documentation

Documentation for the rdf-go RDF parsing/encoding library

View the Project on GitHub geoknoesis/rdf-go

API Reference

Complete reference documentation for rdf-go.

Types

Term Interface

type Term interface {
    Kind() TermKind
    String() string
}

Term is the interface that all RDF terms implement.

TermKind

type TermKind uint8

const (
    TermIRI TermKind = iota
    TermBlankNode
    TermLiteral
    TermTriple
)

TermKind identifies the type of an RDF term.

IRI

type IRI struct {
    Value string
}

IRI represents an RDF IRI (Internationalized Resource Identifier).

Methods:

BlankNode

type BlankNode struct {
    ID string
}

BlankNode represents an RDF blank node.

Methods:

Literal

type Literal struct {
    Lexical  string
    Datatype IRI
    Lang     string
}

Literal represents an RDF literal value.

Fields:

Methods:

TripleTerm

type TripleTerm struct {
    S Term
    P IRI
    O Term
}

TripleTerm represents an RDF-star quoted triple.

Methods:

Triple

type Triple struct {
    S Term
    P IRI
    O Term
}

Triple represents an RDF triple (subject, predicate, object).

Methods:

Quad

type Quad struct {
    S Term
    P IRI
    O Term
    G Term
}

Quad represents an RDF quad (triple with optional graph name).

Methods:

Statement

type Statement struct {
    S Term
    P IRI
    O Term
    G Term  // nil for triples, non-nil for quads (can be omitted, defaults to nil)
}

Statement represents an RDF statement, which can be either a triple or a quad. If G is nil (or omitted), it represents a triple. If G is non-nil, it represents a quad.

Creating Statements:

// Option 1: Omit G (defaults to nil for triples)
stmt := rdf.Statement{
    S: rdf.IRI{Value: "http://example.org/s"},
    P: rdf.IRI{Value: "http://example.org/p"},
    O: rdf.IRI{Value: "http://example.org/o"},
    // G omitted - defaults to nil (triple)
}

// Option 2: Use convenience function
stmt := rdf.NewTriple(
    rdf.IRI{Value: "http://example.org/s"},
    rdf.IRI{Value: "http://example.org/p"},
    rdf.IRI{Value: "http://example.org/o"},
)

// For quads, specify G
quad := rdf.Statement{
    S: rdf.IRI{Value: "http://example.org/s"},
    P: rdf.IRI{Value: "http://example.org/p"},
    O: rdf.IRI{Value: "http://example.org/o"},
    G: rdf.IRI{Value: "http://example.org/g"},
}

// Or use convenience function
quad := rdf.NewQuad(
    rdf.IRI{Value: "http://example.org/s"},
    rdf.IRI{Value: "http://example.org/p"},
    rdf.IRI{Value: "http://example.org/o"},
    rdf.IRI{Value: "http://example.org/g"},
)

Methods:

Format

type Format string

const (
    FormatAuto Format = ""
    
    // Triple formats
    FormatTurtle   Format = "turtle"
    FormatNTriples Format = "ntriples"
    FormatRDFXML   Format = "rdfxml"
    FormatJSONLD   Format = "jsonld"
    
    // Quad formats
    FormatTriG   Format = "trig"
    FormatNQuads Format = "nquads"
)

Format represents an RDF serialization format.

Methods:

Interfaces

Reader

type Reader interface {
    Next() (Statement, error)
    Close() error
}

Reader streams RDF statements from an input. A statement can be either a triple (G is nil) or a quad (G is non-nil).

Methods:

Writer

type Writer interface {
    Write(Statement) error
    Flush() error
    Close() error
}

Writer streams RDF statements to an output. For triple-only formats, the graph (G) field is ignored.

Methods:

Handler

type Handler func(Statement) error

Handler processes statements in push mode. It’s a function type that can be passed to Parse.

Functions

NewReader

func NewReader(r io.Reader, format Format, opts ...Option) (Reader, error)

NewReader creates a reader for the specified format. If format is FormatAuto (empty string), the format is automatically detected. Auto-detection reads from the reader, so the reader position will be advanced.

Parameters:

Returns:

Example:

reader, err := rdf.NewReader(reader, rdf.FormatTurtle)

NewWriter

func NewWriter(w io.Writer, format Format, opts ...Option) (Writer, error)

NewWriter creates a writer for the specified format.

Parameters:

Returns:

Example:

writer, err := rdf.NewWriter(writer, rdf.FormatNTriples)

Parse

func Parse(ctx context.Context, r io.Reader, format Format, handler Handler, opts ...Option) error

Parse parses RDF from the reader and streams statements to the handler. If format is FormatAuto (empty string), the format is automatically detected.

Parameters:

Returns:

Example:

err := rdf.Parse(context.Background(), reader, rdf.FormatTurtle, func(s rdf.Statement) error {
    // process statement
    return nil
})

ParseFormat

func ParseFormat(s string) (Format, bool)

ParseFormat normalizes a format string and returns a Format. Supports common aliases (e.g., “ttl” -> FormatTurtle, “nt” -> FormatNTriples).

Parameters:

Returns:

Supported aliases:

Example:

format, ok := rdf.ParseFormat("ttl")

Options

Options configure reader/writer behavior using functional options.

Option

type Option func(*Options)

Option is a function that modifies Options.

Options

type Options struct {
    Context context.Context
    
    // Security limits for untrusted input
    MaxLineBytes      int
    MaxStatementBytes int
    MaxDepth          int
    MaxTriples        int64
    
    // Format-specific options
    AllowQuotedTripleStatement bool
    DebugStatements            bool
}

Options configures parser/writer behavior.

Option Functions

Example:

reader, err := rdf.NewReader(reader, rdf.FormatTurtle,
    rdf.OptSafeLimits(),
    rdf.OptMaxDepth(50),
    rdf.OptContext(ctx),
)

Errors

ErrUnsupportedFormat

var ErrUnsupportedFormat = errors.New("unsupported format")

ErrUnsupportedFormat is returned when a format is not supported by NewReader or NewWriter.

ParseError

type ParseError struct {
    Format  string
    Line    int
    Column  int
    Offset  int
    Err     error
    Message string
}

ParseError represents a parsing error with position information.

Methods:

Best Practices

  1. Always close readers and writers: Use defer reader.Close() or defer writer.Close()

  2. Handle EOF properly: Check for io.EOF when Next() returns an error

  3. Use streaming for large files: Use NewReader or Parse for efficient processing of large inputs

  4. Check format support: Use ParseFormat to validate format strings before creating readers/writers

  5. Use Statement type: The unified Statement type works with all formats. Use stmt.IsTriple() or stmt.IsQuad() to check the statement type.

  6. Use context for cancellation: Pass a context to Parse to enable cancellation

  7. Flush writers: Call Flush() before Close() to ensure all data is written

  8. Set security limits: For untrusted input, always use OptSafeLimits() or set explicit limits