Register a New Namespace

Register a New Namespace #

Problem #

I need a new namespace, how can I create one?

Solution #

The next code registers a new namespace:

package main

import (
	"fmt"
	"github.com/cayleygraph/quad/voc"

	// Add built-in vocabulary
	_ "github.com/cayleygraph/quad/voc/rdfs"

	// Add own vocabulary
	_ "github.com/tombenke/cayley-cookbook-src/kbase/voc/foaf"
)

func init() {
	// Register a new namespace with prefix
	voc.RegisterPrefix(`acc:`, `http://mycompany.com/voc/accounting#`)
}

func main() {
	fmt.Println("Registered namespaces:")
	for _, v := range voc.List() {
		fmt.Printf("  %s => %s\n", v.Prefix, v.Full)
	}
}

Run this code on Repl.it

    cd namespaces
    go run register_namespace.go

The results:

Registered namespaces:
  rdfs: => http://www.w3.org/2000/01/rdf-schema#
  foaf: => http://xmlns.com/foaf/0.1/#
  acc: => http://mycompany.com/voc/accounting#