println and other builtin functions

println is a simple yet powerful builtin function. It can print anything: strings, numbers, arrays, maps, structs.

  1. struct User{ name string age int }
  2. println(1) // "1"
  3. println('hi') // "hi"
  4. println([1,2,3]) // "[1, 2, 3]"
  5. println(User{name:'Bob', age:20}) // "User{name:'Bob', age:20}"

If you want to define a custom print value for your type, simply define a .str() string method:

  1. struct Color {
  2. r int
  3. g int
  4. b int
  5. }
  6. pub fn (c Color) str() string {
  7. return '{$c.r, $c.g, $c.b}'
  8. }
  9. red := Color{
  10. r: 255
  11. g: 0
  12. b: 0
  13. }
  14. println(red)

If you don’t want to print a newline, use print() instead.

The number of builtin functions is low. Other builtin functions are:

  1. fn exit(exit_code int) // terminate the program
  2. fn panic(message string)
  3. fn print_backtrace()
  4. fn eprintln(s string) // same as println, but use stderr