Loading Cue

Loading and printing Cue code

This first example shows how to load, build, and validate Cue.


package main

import (


func main() {
	// We need a Cue.Runtime, the zero value is ready to use
	var RT cue.Runtime

	// The entrypoints are the same as the files you'd specify at the command line
	entrypoints := []string{"hello.cue"}

	// Load Cue files into Cue build.Instances slice
	// the second arg is a configuration object, we'll see this later
	bis := load.Instances(entrypoints, nil)

	// Loop over the instances, checking for errors and printing
	for _, bi := range bis {
		// check for errors on the instance
		// these are typically parsing errors
		if bi.Err != nil {
			fmt.Println("Error during load:", bi.Err)

		// Use cue.Runtime to build.Instance to cue.INstance
		I, err := RT.Build(bi)
		if err != nil {
			fmt.Println("Error during build:", bi.Err)

		// get the root value and print it
		value := I.Value()
		fmt.Println("root value:", value)

		// Validate the value
		err = value.Validate()
		if err != nil {
			fmt.Println("Error during validate:", err)



package hello

hello: "world"

#A: {
	foo: string

// to cause a load error, remove the '&'
// to cause a build error, change '#A' to '#B'
// to cause a validation error, change foo to '1'
a: #A & {
	foo: "bar"

The Loading Configuration

Cue’s load.Instances has a second argument for configuring options. The CLI, it uses the command, flags, and args to set these values. When we load with Go, we can setup this Config object to modify the behavior.

load.Config documentation

The comments there explain how to use the vaious options. Most are for advanced use cases and will make more sense once you have seen more of Cue. Others you have seen as flags to the CLI.

2021 Hofstadter, Inc