go

Syntax

go

Details

In DolphinDB, the entire script submitted for execution is parsed before execution starts. The go statement splits the script into blocks to parse and execute these blocks one by one. The system parses and executes the first block, then parses and executes the second block, and etc.

When parsing, a variable or function must be explicitly defined to be referenced in subsequent code. Some variables or functions are dynamically registered during the execution of functions such as share, enableTableShareAndPersistence, loadPlugin and run, but are not registered during the parsing phase. As a result, if the subsequent code references these dynamically registered variables or functions, we must use the go statement before the subsequent code. Otherwise, exceptions indicating undefined variables or functions will be thrown in parsing subsequent code.

Note: A DolphinDB statement or function must be fully parsed to be executed. Therefore, the go statement in conditional statements, loops and other nested statements or function bodies will not take effect.

Examples

When the following code is executed, the system will throw an exception indicating that the variable is undefined.

$ t=table(rand(`WMI`PG`TSLA,100) as sym, rand(1..10, 100) as qty, rand(10.25 10.5 10.75, 100) as price)
$ share(t,`st)
$ insert into st values(`AAPL,50,10.25);
Syntax Error: [line #3] Can't recognize table st

As the entire script was parsed together before execution and during parsing the share statement has not generated the object “st”, the insert statement cannot find the object “st”. For cases like this, we can use the go statement after the share statement to split the script into 2 parts.

$ t=table(rand(`WMI`PG`TSLA,100) as sym, rand(1..10, 100) as qty, rand(10.25 10.5 10.75, 100) as price)
$ share(t,`st)
$ go;
$ insert into st values(`AAPL,50,10.25);

DolphinDB fisrt parses and executes the first code block before the go statement to share table t as st, then parses and executes the second code block to insert values into st.

In the following code block, during the parsing phase, the variable fs is registered, but the function body f2() is not. Therefore, an error indicating undefined variables would occur as the system goes on to parse the rest of the script.

$ fs = ["def f2(){return 'haha1';}", "def f2(){return 'haha2';}", "def f2(){return 'haha3';}"];
$ runScript(fs[2]);
$ print(f2());
Syntax Error: [line #25] Cannot recognize the token f2

Use the go statement to split the code and let the system parse the first block to generate the dynamic variable f2().

$ fs = ["def f2(){return 'haha1';}", "def f2(){return 'haha2';}", "def f2(){return 'haha3';}"];
$ runScript(fs[2]);
$ go
$ print(f2());
haha3

go statement doesn’t work inside a for-loop statement.

$ fs = ["def f2(){return 'haha1';}", "def f2(){return 'haha2';}", "def f2(){return 'haha3';}"];
$ for(s in fs){runScript(s); go; print(f2());}

In the following example, variable a is defined in the file test.txt via a=100. The run command is used to execute the file, and then variable a is referenced. If we don’t use the go statement after the run command, an error would occur during the parsing phase indicating that a is not defined.

$ run("D:/test.txt")
$ print(a)
Syntax Error: [line #27] Cannot recognize the token a

$ run("D:/test.txt")
$ go
print(a)