Amalegeni-Go > Step-by-step | |||||||||||
Amalegeni-Go | |||||||||||
Primitive : Ex 1 Struct : Ex 2 Map : Ex 3 Copy : Ex 4 Json Grab : Ex 5 CSV Load: Ex 6 |
Example 2 : returning a slice of structsIn Example 1 you saw how a function can return a single value or a slice. Here you'll see how to have functions return a slice of records. Just define the struct holding the record as you are used to writing structs in Go:
Note: this looks a lot like plain go, but there are restrictions like it has to be a 'flat' structure, you can't nest structures within structures. A function returning a slice of these structs is written as follows, no big surprise here:
And then the code calling your SelectMaster() function can look like the following:
Line 12 is where the function gets called. Like before: all the above code is pretty self-explanatory, ask for more detail if need be. Example 2 testingAmelegeni-go can generate the test code for you, just add a run{{ }} clause to your function, with an appropriate parameter, like this here (see line 7-10):
The parameter name has to match the parameter name in the declaration of the function. Instructions on how to build itThe zipfile ex02.zip contains all the code needed to run this example. PrerequisitesSee the prerequisites of Example 1. The tables in question can be created with this SQL-script: ddl.sql, and populated with this script: data.sql InstructionsDownload and unpack the ex02.zip, and like before, edit the 'setenv.sh' script and execute it:
Run amgo (amalegeni-go) on the ex02.amg file:
As you can guess, the Master struct is defined in the ..struct.go file, the ..func.go file contains the function code, and finally the test code is contained in the ..test.go file. Running the calling code of ./src/run/ex02/ex02_main.go is done like this:
The other way of executing the function is via running go-test:
Simple, no? | ||||||||||
© Willem Moors, 2013 - 2020 |