println and other builtin functions
println
is a simple yet powerful builtin function. It can print anything: strings, numbers, arrays, maps, structs.
struct User{ name string age int }
println(1) // "1"
println('hi') // "hi"
println([1,2,3]) // "[1, 2, 3]"
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:
struct Color {
r int
g int
b int
}
pub fn (c Color) str() string {
return '{$c.r, $c.g, $c.b}'
}
red := Color{
r: 255
g: 0
b: 0
}
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:
fn exit(exit_code int) // terminate the program
fn panic(message string)
fn print_backtrace()
fn eprintln(s string) // same as println, but use stderr