Miscellaneous Features

This guide covers various useful features of Golo-Script, including input/output handling, environment variables, ANSI codes for console coloring, and comments.

Input/Output

Basic Output

Golo-Script provides two main functions for output:

# println - prints with a newline
println("Hello, Golo!")
println("Line 1")
println("Line 2")

# print - prints without a newline
print("This is")
print(" a single")
print(" line")
println()  # Newline at the end

Printing Multiple Values

Both functions accept multiple arguments:

let name = "Alice"
let age = 30
println("Name:", name, "Age:", age)
# Output: Name: Alice Age: 30

Reading User Input

The readln() function reads a line from standard input:

# With separate prompt
print("Enter your name: ")
let name = readln()

# With inline prompt
let city = readln("Enter your city: ")

# Read empty input (like pressing Enter to continue)
print("Press Enter to continue...")
readln()

Complete Interactive Example

function main = |args| {
  println("=== Interactive Greeting Program ===")
  println()

  let name = readln("What is your name? ")
  let city = readln("Where are you from? ")

  println()
  println("Nice to meet you,", name, "from", city, "!")
  println()

  print("Press Enter to continue...")
  readln()

  println("Goodbye!")
}

Environment Variables

Golo-Script provides simple functions for working with environment variables.

Reading Environment Variables

Use getenv(name) to read an environment variable:

# Read an existing environment variable
let home = getenv("HOME")
println("HOME =", home)

let path = getenv("PATH")
println("PATH =", path)

# Read a non-existent variable (returns empty string)
let nonExistent = getenv("MY_NON_EXISTENT_VAR")
println("MY_NON_EXISTENT_VAR =", nonExistent)  # ""

Setting Environment Variables

Use setenv(name, value) to set an environment variable:

# Set new environment variables
setenv("MY_APP_NAME", "GoloScript")
setenv("MY_APP_VERSION", "1.0.0")
setenv("MY_APP_ENV", "development")

# Read the variables we just set
println("App Name:", getenv("MY_APP_NAME"))
println("Version:", getenv("MY_APP_VERSION"))
println("Environment:", getenv("MY_APP_ENV"))

Modifying Existing Variables

println("Before:", getenv("MY_APP_ENV"))  # development
setenv("MY_APP_ENV", "production")
println("After:", getenv("MY_APP_ENV"))  # production

Practical Configuration Management

function getConfigValue = |envVar, defaultValue| {
  let value = getenv(envVar)
  if value == "" {
    println(envVar, "not set, using default value")
    setenv(envVar, defaultValue)
    return defaultValue
  }
  return value
}

let dbHost = getConfigValue("DB_HOST", "localhost")
let dbPort = getConfigValue("DB_PORT", "5432")

println("Database Host:", dbHost)
println("Database Port:", dbPort)

ANSI Codes for Console

Golo-Script includes a built-in gololang.AnsiCodes module for styling console output with ANSI codes.

Importing the Module

import gololang.AnsiCodes

Text Colors (Foreground)

# Basic colors
fg_black()
print("Black text")
reset()

fg_red()
print("Red text")
reset()

fg_green()
print("Green text")
reset()

fg_yellow()
print("Yellow text")
reset()

fg_blue()
print("Blue text")
reset()

fg_magenta()
print("Magenta text")
reset()

fg_cyan()
print("Cyan text")
reset()

fg_white()
print("White text")
reset()

Important: Always call reset() after styling text to return to default formatting.

Background Colors

bg_red()
fg_white()
print("White on red background")
reset()
println()

bg_green()
fg_black()
print("Black on green background")
reset()
println()

bg_blue()
fg_white()
print("White on blue background")
reset()
println()

Text Styles

# Bold
bold()
print("Bold text")
reset()
println()

# Underline
underscore()
print("Underlined text")
reset()
println()

# Blinking
blink()
print("Blinking text")
reset()
println()

# Reverse video (swap foreground and background)
reverse_video()
print("Reverse video")
reset()
println()

Combined Styles

You can combine multiple styles:

# Bold red
bold()
fg_red()
print("Bold Red")
reset()
println()

# Underscored green
underscore()
fg_green()
print("Underscored Green")
reset()
println()

# Bold yellow on blue background
bold()
fg_yellow()
bg_blue()
print("Bold Yellow on Blue")
reset()
println()

Styled Messages (Success/Error/Warning)

# Success message
bold()
fg_green()
print("βœ“ SUCCESS: ")
reset()
print("Operation completed successfully")
println()

# Error message
bold()
fg_red()
print("βœ— ERROR: ")
reset()
print("Something went wrong")
println()

# Warning message
bold()
fg_yellow()
print("⚠ WARNING: ")
reset()
print("This is a warning")
println()

# Info message
bold()
fg_blue()
print("β„Ή INFO: ")
reset()
print("Here is some information")
println()

Rainbow Text

fg_red()
print("R")
fg_yellow()
print("a")
fg_green()
print("i")
fg_cyan()
print("n")
fg_blue()
print("b")
fg_magenta()
print("o")
fg_red()
print("w")
reset()
println()

Progress Bar

print("Progress: [")

# Completed part (green)
bg_green()
print("          ")
reset()

# Remaining part (gray)
bg_white()
print("          ")
reset()

print("] 50%")
println()

Available Functions

Text Colors (Foreground)

Background Colors

Styles

Comments

Golo-Script supports multi-line comments with the ---- syntax.

Basic Syntax

----
This is a multi-line comment
It can span multiple lines
----

Module-Level Comments

module main

----
Multi-line comment at module level
This demonstrates comments outside functions
----

function add = |a, b| {
  return a + b
}

Comments Inside Functions

function multiply = |x, y| {
  ---- Simple inline comment ----
  return x * y
}

function complexFunction = |data| {
  ----
  Multi-line comment explaining
  complex logic over multiple
  lines
  ----

  let result = processData(data)
  return result
}

Documentation Comments

----
This function adds two numbers
Parameters: a and b
Returns: their sum
----
function add = |a, b| {
  return a + b
}

Comment Best Practices

  1. Use comments to explain the β€œwhy”, not the β€œwhat”

    # Good
    ----
    Uses merge sort algorithm because our datasets
    are typically already partially sorted
    ----
    
    # Avoid
    ---- Sorts the array ----
    
  2. Document complex functions

    ----
    Calculates the similarity score between two documents
    using the Jaccard coefficient.
    
    Parameters:
      - doc1: First document (string)
      - doc2: Second document (string)
    
    Returns: Similarity score between 0.0 and 1.0
    ----
    function calculateSimilarity = |doc1, doc2| {
      # implementation...
    }
    
  3. Use comments for TODOs and notes

    ----
    TODO: Optimize this algorithm for large datasets
    NOTE: This implementation assumes valid inputs
    ----
    

Validation and Checks

Null Checks

let value1 = null
let value2 = "something"

# Check if a value is null
println("value1 is null:", isNull(value1))      # true
println("value2 is null:", isNull(value2))      # false

# Check if a value is not null
println("value1 is not null:", isNotNull(value1))  # false
println("value2 is not null:", isNotNull(value2))  # true

Validation with require

The require(value, message) function throws an exception if the value is null:

function validateInput = |input| {
  try {
    let validated = require(input, "Input cannot be null!")
    println("Valid input:", validated)
    return validated
  } catch (e) {
    println("Validation failed:", e)
    return null
  }
}

validateInput("Hello")  # Success
validateInput(null)     # Throws exception

Practical Use in Functions

function processUser = |name, email| {
  # Validate required inputs
  require(name, "Name is required")
  require(email, "Email is required")

  println("Processing user:", name, email)
  # Continue processing...
}

# Usage
try {
  processUser("Alice", "alice@example.com")  # OK
  processUser(null, "bob@example.com")       # Throws exception
} catch (e) {
  println("Error:", e)
}

File Existence Check

if fileExists("config.json") {
  println("Configuration file found")
  let config = readFile("config.json")
  # Process configuration...
} else {
  println("Configuration file missing")
  # Create default configuration...
}

Quick Reference Functions

Input/Output

Function Description
print(...) Prints values without newline
println(...) Prints values with newline
readln() Reads a line from standard input
readln(prompt) Displays a prompt and reads a line

Environment Variables

Function Description
getenv(name) Reads an environment variable (returns β€œβ€ if non-existent)
setenv(name, value) Sets an environment variable

Validation

Function Description
isNull(value) Returns true if value is null
isNotNull(value) Returns true if value is not null
require(value, message) Throws an exception if value is null

File Checks

Function Description
fileExists(path) Checks if a file or directory exists

Β© 2026 GoloScript Project | Built with Gu10berg

Subscribe: πŸ“‘ RSS | βš›οΈ Atom