Browse Docs

Golang

In this section

  • ๐Ÿ Cobra

    A Command Builder for Go

    Usefull

    • Installation
    1# Install GO
    2GO_VERSION="1.21.0"
    3wget https://go.dev/dl/go${GO_VERSION}.linux-amd64.tar.gz
    4sudo tar -C /usr/local -xzf go${GO_VERSION}.linux-amd64.tar.gz
    5export PATH=$PATH:/usr/local/go/bin
    6
    7# Install Cobra - CLI builder
    8go install github.com/spf13/cobra-cli@latest
    9sudo cp -pr ./go /usr/local/.
    
    • Init
    1mkdir -p ${project} && cd ${project}
    2go mod init ${project}
    3cobra-cli init
    4go build
    5go install
    6cobra-cli add timezone
    
  • ๐Ÿ‘ฎ CUE-lang

    CUE stands for Configure, Unify, Execute

    Basics

    • Installation
     1# Install GO
     2GO_VERSION="1.21.0"
     3wget https://go.dev/dl/go${GO_VERSION}.linux-amd64.tar.gz
     4sudo tar -C /usr/local -xzf go${GO_VERSION}.linux-amd64.tar.gz
     5export PATH=$PATH:/usr/local/go/bin
     6
     7go install cuelang.org/go/cmd/cue@latest
     8sudo cp -pr ./go /usr/local/.
     9
    10# or use Container
    11printf "\e[1;34m[INFO]\e[m Install CUElang:\n";    
    12podman pull docker.io/cuelang/cue:latest
    
    • concepts

    top -> schema -> constraint -> data -> bottom

    • Command
     1# import a file 
     2cue import imageset-config.yaml 
     3
     4# Validate 
     5cue vet imageset-config.cue imageset-config.yaml
     6
     7
     8* Some basics example
     9
    10```go
    11// This is a comment
    12_greeting: "Welcome" // Hidden fields start with "_"
    13#project:  "CUE"     // Definitions start with "#"
    14
    15message: "\(_greeting) to \(#project)!" // Regular fields are exported
    16
    17#Person: {
    18  age: number            // Mandatory condition and must be a number
    19  hobbies?: [...string]  // non mandatory but if present must be a list of string
    20}
    21
    22// Constrain which call #Person and check if age
    23#Adult: #Person & {
    24  age: >=18
    25}
    26
    27// =~ match a regular expression
    28#Phone: string & =~ "[0-9]+"
    29
    30// Mapping
    31instanceType: {
    32    web: "small"
    33    app: "medium"
    34    db:  "large"
    35}
    36
    37server1: {
    38    role:     "app"
    39    instance: instanceType[role]
    40}
    41
    42// server1.instance: "medium"
    
    • Scripting
    1# executable have extension name "_tool.cue"
    2
    3# usage
    4cue cmd prompter
    
     1package foo
     2
     3import (
     4	"tool/cli"
     5	"tool/exec"
     6	"tool/file"
     7)
     8
     9// moved to the data.cue file to show how we can reference "pure" Cue files
    10city: "Amsterdam"
    11
    12// A command named "prompter"
    13command: prompter: {
    14
    15	// save transcript to this file
    16	var: {
    17		file: *"out.txt" | string @tag(file)
    18	} // you can use "-t flag=filename.txt" to change the output file, see "cue help injection" for more details
    19
    20	// prompt the user for some input
    21	ask: cli.Ask & {
    22		prompt:   "What is your name?"
    23		response: string
    24	}
    25
    26	// run an external command, starts after ask
    27	echo: exec.Run & {
    28		// note the reference to ask and city here
    29		cmd: ["echo", "Hello", ask.response + "!", "Have you been to", city + "?"]
    30		stdout: string // capture stdout, don't print to the terminal
    31	}
    32
    33	// append to a file, starts after echo
    34	append: file.Append & {
    35		filename: var.file
    36		contents: echo.stdout // because we reference the echo task
    37	}
    38
    39	// also starts after echo, and concurrently with append
    40	print: cli.Print & {
    41		text: echo.stdout // write the output to the terminal since we captured it previously
    42	}
    43}
    

    Sources

    Offical Documentation

Thursday, January 15, 2026 Monday, January 1, 1