Language Tour
Welcome to the goose language tour! This tour will introduce you to the basic concepts of goose and show you how to write your first goose program. Let's get started!
Hello, World!
Let's start with the classic "Hello, World!" program. In goose, you can
print text to the console using the
println
function.
Here's how you can write the "Hello, World!" program in goose:
println("Hello, World!")
To run the program, save the code to a file named main.goose
and run the following command in your terminal:
goose run main.goose
You should see Hello, World!
printed to the console.
Congratulations! You've just written your first goose program.
Variables
Variables are type-agnostic containers that store data. You can create
variables in goose using the let
keyword. Give the variable a
name and assign it a value using the =
operator. Later, you can
use the same =
operator to change the value stored inside.
let message = "Hello, World!"
println(message)
message = "Goodbye, World!"
println(message)
In this example, we create a variable named message
and assign
it the value "Hello, World!"
. We then print the value of the
variable to the console. Next, we change the value of the variable to
"Goodbye, World!"
and print it again.
You can also make final variables using the const
keyword.
Final variables cannot be reassigned once they are initialized.
const message = "Hello, World!"
println(message)
message = "Goodbye, World!" // Cannot reassign a const variable
For more information on variables and data types in goose, see the Variables section of the language reference.
Comments
Comments are used to document your code and provide context to other
developers (or your future self). In goose, comments start with the
//
characters and continue until the end of the line.
// This is a single-line comment
let message = "Hello, World!" // This is another single-line comment
You can also write multi-line comments using the /*
and
*/
characters.
/*
This is a multi-line comment
that spans multiple lines
*/
Data types
goose comes with several built-in data types that you can use to store different kinds of values. Here are some of the basic ones:
string
: A sequence of charactersint
: A whole number (arbitrary precision)float
: A floating-point numberbool
: A boolean value (eithertrue
orfalse
)symbol
: A unique identifierarray
: An ordered list of valuesobject
: An unordered collection of keys and values
For more information on data types in goose, see the Data Types section of the language reference.
Functions
Functions are blocks of code that perform a specific task. You can define
functions in goose using the fn
keyword. Give the function a
name, a list of parameters, and a block of code to execute when the function
is called. Function blocks, like with other blocks in goose, are delimited
by the end
keyword.
fn greet(name)
println("Hello, $name!")
end
greet("Alice")
greet("Bob")
For more information on functions in goose, see the Functions section of the language reference.
Control flow
Control flow statements allow you to change the order in which your code is
executed. goose supports several control flow statements, including
if
, else
, while
, and
for
.
let x = 10
if x > 5
println("x is greater than 5")
else
println("x is less than or equal to 5")
end
let i = 0
repeat while i < 5
println(i)
i = i + 1
end
// repeats forever, until a break statement is encountered
repeat forever
println("Hello, World!")
break
end
// repeats 10 times!
repeat 10 times
println("Hello, World!")
end
for j in [1, 2, 3, 4, 5]
println(j)
end
For more information on control flow in goose, see the Control Flow section of the language reference.
User input
goose has a rich standard library which includes functions for reading user
input from the console. You can use read()
or
prompt()
from std:readline
to read input from the
user.
To use them, you'll need to import the std:readline
module at
the top of the file. Once you do, the readline
object will be
available for you to use. Then, use a dot to access the read()
or prompt()
functions.
import "std:readline"
let name = readline.prompt("What is your name? ")
println("Hello, $name!")
There's plenty more in the standard library, so be sure to check out the API Reference for a full list of available modules and functions.
That's the basics
That's it for the goose language tour! You've learned the basics of goose and are ready to start writing your own programs. If you have any questions or need help, feel free to ask in the community. Happy coding!