packagemainimport("fmt""cuelang.org/go/cue/cuecontext""cuelang.org/go/cue/load")funcmain(){// We need a cue.Context for building after loadingctx:=cuecontext.New()// The entrypoints are the same as the files you'd specify at the command lineentrypoints:=[]string{"hello.cue"}// Load Cue files into Cue build.Instances slice// the second arg is a configuration object, we'll see this laterbis:=load.Instances(entrypoints,nil)// Loop over the instances, typically there is only onefor_,bi:=rangebis{// check for errors on the instance// these are typically parsing errorsifbi.Err!=nil{fmt.Println("Error during load:",bi.Err)continue}// Use cue.Context.BuildInstance to turn// a build.Instance into a cue.Valuevalue:=ctx.BuildInstance(bi)ifvalue.Err()!=nil{fmt.Println("Error during build:",value.Err())continue}// Validate the valueerr:=value.Validate()iferr!=nil{fmt.Println("Error during validation:",err)continue}// Print the valuefmt.Println("value:",value)}}
hello.cue
packagehellohello: "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"}