How do I print output to the console?

← Getting Started · Ref: Q3

Use the built-in Stdout type:

  stdout <- Stdout()
  stdout.println("Hello, World!")

println() adds a newline, print() does not:

  stdout.print("no newline")
  stdout.println(" with newline")

String interpolation uses backticks with ${}:

  name <- "Steve"
  stdout.println(`Hello ${name}`)

Concatenation uses the + operator:

  stdout.println("Value is [" + name + "]")

Any type with a $ (string) operator can be printed directly.

For error/diagnostic output, use Stderr instead:

  stderr <- Stderr()
  stderr.println("Something went wrong")

In stream pipelines, stdout is a sink:

  cat items > stdout

For the full Stdout API, run: ek9 -h Stdout

See Q4 for reading input. See Q37 for string interpolation and methods. See Q43 for escaping in interpolated strings.

Example

defines module qa.getting.started.print.output

  defines program
    PrintOutput()
      stdout <- Stdout()

      stdout.println("Hello, World!")
      stdout.print("no ")
      stdout.println("newline needed")

      name <- "EK9"
      stdout.println(`Welcome to ${name}`)
      stdout.println(`Language: [${name}]`)

Common mistakes

E50001 — A typo in the variable name 'nme' means the compiler cannot find any identifier with that name. The correct name is 'name'. EK9 is case-sensitive. See ek9 -h E50001 for details.

Incorrect:

stdout.println(`Welcome to ${nme}`)

Correct:

stdout.println(`Welcome to ${name}`)
Other ways to ask this
  • How to print text in EK9
  • EK9 equivalent of println
  • How to display output in EK9

Coming from another language?

Python: print(), Java: System.out.println(), Rust: println!(), Go: fmt.Println(), C++: std::cout <<. EK9 uses an object-based approach — create Stdout() and call methods on it.

Keywords: text, beginner, output, console, stdout, first, print, screen, write, start, println, intro, display