CUE supports the fmt.Printf modifiers
for when you want to quickly print values.
For fine grained control when printing,
you can use the cue/format package.
Printing with go/fmt
print.go
packagemainimport("fmt""cuelang.org/go/cue""cuelang.org/go/cue/cuecontext")constval=`
i: int
s: string
t: [string]: string
_h: "hidden"
#d: foo: "bar"
`funcmain(){var(c*cue.Contextvcue.Value)// create a contextc=cuecontext.New()// compile some CUE into a Valuev=c.CompileString(val)// print the valuefmt.Printf("// %%v\n%v\n\n// %%# v\n%# v\n",v,v)}
When we run cue eval and cue def,
we have more options to control what gets printed through the flags.
Those flags map onto the cue.Option type which is
supplied to cue.Value.Syntax, a function that returns the
Abstract Syntax Tree (AST) for the value.
We then use Cue’s format.Node to pretty print a Cue AST.
format.go
packagemainimport("fmt""cuelang.org/go/cue""cuelang.org/go/cue/cuecontext""cuelang.org/go/cue/format")constval=`
i: int
s: string
t: [string]: string
_h: "hidden"
#d: foo: "bar"
`funcmain(){var(c*cue.Contextvcue.Value)// create a contextc=cuecontext.New()// compile some CUE into a Valuev=c.CompileString(val)// Generate an AST// try out different optionssyn:=v.Syntax(cue.Final(),// close structs and listscue.Concrete(false),// allow incomplete valuescue.Definitions(false),cue.Hidden(true),cue.Optional(true),cue.Attributes(true),cue.Docs(true),)// Pretty print the AST, returns ([]byte, error)bs,_:=format.Node(syn,// format.TabIndent(false),// format.UseSpaces(2),)// print to stdoutfmt.Println(string(bs))}