Amalegeni-Go
 

Example 2 : returning a slice of structs

In 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:

1
2
3
4
5
type Master struct {
    Id string
    Artist string 
    Album string
} 

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:

1
2
3
4
5
6
func SelectMaster(artist_like string) []Master
<<  
    select id,artist,album 
    from t_master
    where artist like $1
>>  

And then the code calling your SelectMaster() function can look like the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package main

import ( 
    "fmt" 
    "ex02"
) 

func main() { 
      
    artist_like:="%a%"

    list, err:= ex02.SelectMaster(artist_like)
    if err!=nil {
        fmt.Printf("Error: %v\n",err)
        return
    }

    for _,el:= range (list) {  
        fmt.Printf("%+v\n",el)
    }
}    

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 testing

Amelegeni-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):

1
2
3
4
5
6
7
8
9
10
func SelectMaster(artist_like string) []Master
<<  
    select id,artist,album 
    from t_master
    where artist like $1
>>  
run 
{{  
    artist_like:="%app%"
}}

The parameter name has to match the parameter name in the declaration of the function.

Instructions on how to build it

The zipfile ex02.zip contains all the code needed to run this example.

Prerequisites

See 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

Instructions

Download and unpack the ex02.zip, and like before, edit the 'setenv.sh' script and execute it:

$ unzip ex02.zip 
$ cd plain 
$ vi setenv.sh 
$ . ./setenv.sh

Run amgo (amalegeni-go) on the ex02.amg file:

$ amgo ex02.amg 
template/se.tpl * src/ex02/ex02.amg 
Generating: src/ex02/ex02_generated_type.go
Generating: src/ex02/ex02_generated_func.go
Generating: src/ex02/ex02_generated_test.go

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:

$ go install run/ex02
$ ex02
{Id:2a087a04 Artist:Frank Zappa Album:Waka Jawaka}
{Id:f40d3812 Artist:Johann Sebastian Bach Album:Suites for Cello}

The other way of executing the function is via running go-test:

$ go test ex02 -test.v
=== RUN TestSelectMaster
[{2a087a04 Frank Zappa Waka Jawaka}]
--- PASS: TestSelectMaster (0.02 seconds)
PASS
ok      ex02    0.028s

Simple, no?

 
© Willem Moors, 2013 - 2020