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. }

7. pub fn (c Color) str() string {
8. return '{$c.r, $c.g, $c.b}'
9. }

11. red := Color{
12. r: 255
13. g: 0
14. b: 0
15. }
16. 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