Call handler

Purpose: Call another handler within the same process.

call-handler <request path> [ return-value  <return value> ]

Calls another handler within the same request in the same process. You can call any handler within the same application. "Calling a handler" means executing it solely within the context of the top handler currently running; no before-handler or after-handler will execute for the called handler.

<request path> is the request path served by the handler being called. It can be a string variable or a constant.

Use set-param and get-param to pass parameters between the caller and callee handlers.

You can obtain a <return value> from the called handler <request path> by using "return-value" clause. See return-handler on how to return a number value from <request path> handler.

call-handler uses the same high-performance hash table used by a request to route requests by name; if <request path> is a constant string, then a hash table lookup is performed only once for the life of the process and all subsequent calls use a cached address of the request handler.
Call depth
There is a limit on the call depth, including recursion in order to prevent application and system stability issues. See get-app and set-app with "stack-depth" clause for details and on how to change the defaults.
Examples
The following example demonstrate calling a call-handler twice, and also using its output inline in the caller. An input parameter is passed to it, and an output obtained:

Copy to file "callsub.golf":
%% /callsub public
    //
    // First call to call-handler
    //
    // Set input for call-handler
    set-param inp = "some string"
    (( s
    call-handler "/sub/service"
    ))
    // Get output from call-handler
    get-param out type string
    @<<print-out s>> with output [<<print-out out>>]

    //
    // Second call to call-handler
    //
    // Set input for call-handler called as inline code
    set-param inp = "new string"
    (( s
    @Output: <<call-handler "/sub/service">>
    ))
    // Get output from call-handler
    get-param out type string
    @<<print-out s>> with output [<<print-out out>>]
%%

And in "sub/service.golf" file (meaning file "service.golf" in subdirectory "sub"):
%% /sub/service private
    @This is sub!
    get-param inp
    (( out
    @got input: <<print-out inp>>
    ))
    set-param out = out
%%

Create and build an application:
gg -k subhandler
gg -q

Run it:
gg -r --req="/callsub" --exec --silent-header

The output:
This is sub! with output [got input: some string]
Output: This is sub! with output [got input: new string]

See also
Program flow
break-loop  
call-handler  
code-blocks  
continue-loop  
do-once  
exit-handler  
if-defined  
if-true  
quit-process  
return-handler  
start-loop  
Service processing
after-handler  
before-handler  
begin-handler  
call-handler  
See all
documentation


Copyright (c) 2019-2025 Gliim LLC. All contents on this web site is "AS IS" without warranties or guarantees of any kind.