Skip to content
Sword

Running a program

import "std/io"
func main() !int {
try io.Print("hello\n")
return 0
}
Terminal window
shield hello.sword -o hello
./hello

main returns int, which becomes the process exit status. The ! in front means it can fail; more on that later. If you have nothing to say to the outside world, func main() int is fine on its own.

There are no semicolons. The lexer inserts them at line ends, the same way Go does, so a statement ends where the line does.

To print something other than a string, Printf takes values of any type:

import "std/io"
func main() !int {
count := 42
ratio := 1.5
try io.Printf("{} items, {} each\n", count, ratio)
try io.Println("or just this:", count, ratio, true)
return 0
}

{} takes the next argument. There are no type letters like %d, because there is nothing for them to do — the argument already knows what it is, so a format string cannot disagree with it. {x} asks for hex and {.3} for a float with three places.

Neither of these needs an allocator. They format into a block of stack and flush as it fills, which matters because printing is the first thing anybody writes.