Skip to main content

main

Attribute Macro main 

Source
#[main]
Expand description

Attribute macro that runs a function body as the program’s first goroutine.

#[go_lib::main]
fn main() {
    let (tx, rx) = go_lib::chan::chan::<i32>(0);
    go_lib::go!(move || tx.send(42));
    println!("{}", rx.recv().unwrap());
}

The scheduler is a process-wide singleton, initialised on first use; the attribute promotes the body into the “main goroutine” and blocks the calling thread until it returns. See the go_lib_macros::main documentation for the full expansion rules and return-type support. Attribute macro that promotes a function’s body into the program’s first goroutine on the process-wide go-lib scheduler.

This is the single blessed entry point: apply it to fn main (or any entry function) instead of manually wrapping the body. The scheduler is a process singleton, initialised on first use; the attribute runs the body as the “main goroutine” and blocks the calling OS thread until it returns, mirroring Go’s main.

The macro rewrites

#[go_lib::main]
fn main() {
    /* body */
}

into

fn main() {
    go_lib::__main_entry(move || {
        /* body */
    })
}

When the function has a return type the closure is annotated with the same type so that ? and explicit return expressions work as expected:

#[go_lib::main]
fn main() -> Result<(), MyError> {
    do_work()?;
    Ok(())
}
// expands to:
fn main() -> Result<(), MyError> {
    go_lib::__main_entry(move || -> Result<(), MyError> {
        do_work()?;
        Ok(())
    })
}

Function parameters (if any) are captured by the move closure, so the macro also works on non-main entry points or helper functions.

§Errors

Emits a compile error if the function is async (go-lib provides its own concurrency model and does not interact with an async executor).