Skip to main content

Revamp Go Code Samples for Inline Initialization

· 8 min read

We have implemented several enhancements to the Go code samples, improving the developer experience by enabling developers to initialize inline code without encountering errors and to get started quickly.

Details

This release introduces significant improvements to the code samples, especially for initializing parameters, generated for your API. We've made several updates to ensure that the code samples are idiomatic, easier to understand, and production-ready.

The new code samples are designed to enhance the developer experience with inline code initialization, allowing users to grasp the intention of an endpoint regarding the parameters and how they are being initialized. This will enable them to quickly get started with your API by skimming through the code samples

What Has Changed?

To conform to Go’s linter standards, the updated Go code samples undergo linting, specifically through the staticcheck tool (you can find more about it here).

The following major improvements have been made to the Go code samples:

  • Simplified the code samples to make them easier to read and understand by using inline initialization of parameters and proper indentation of code samples.
  • Linting issues were fixed to ensure that code samples comply with the best practices in terms of coding styles and standards.
  • Bugs and errors have been fixed, ensuring that code samples work as expected.

Parameter Initialization Section

Model Initialization

The initialization of model fields has been done inline to ensure the integrity of the object structure and to improve code readability.

model := models.EmployeeInterface(&models.Employee{
    Department:  "department8",
    JoiningDay:  models.Days("Monday"),
    Salary:      240,
    WorkingDays: []models.Days{
        models.Days("Thursday"),
        models.Days("Wednesday"),
        models.Days("Tuesday"),
    },
    HiredAt:     parseTime(time.RFC1123, "Mon, 15 Jun 2009 20:45:30 GMT", func(err error) { log.Fatalln(err) }),
    Dependents:  []models.PersonInterface{
        models.PersonInterface(&models.Person{
            Age:        int64(28),
            Name:       "name8",
            PersonType: models.ToPointer("Per"),
        }),
    },
})

DateTime Initialization

The initialization of DateTime fields has been done inline by creating a wrapper function to ensure the integrity of the object structure and improve code readability.

model := models.Person{
    Name:       "name8",
    Birthday:   parseTime(models.DEFAULT_DATE, "2016-03-13T12:52:32.123Z", func(err error) { log.Fatalln(err) }),
}

Array Initialization

The initialization of array(slice) fields has been done in a single object to ensure the integrity of the object structure and to improve code readability.

mModels := []models.EmployeeInterface{
    models.EmployeeInterface(&models.Employee{
        Salary:      88,
        WorkingDays: []models.Days{
            models.Days("Saturday"),
            models.Days("Sunday"),
        },
        Dependents:  []models.PersonInterface{
            models.PersonInterface(&models.Person{,
                Age:        int64(28),
                Name:       "name8",
                PersonType: models.ToPointer("Per"),
            }),
        },
    }),
}

Map Initialization

The initialization of map fields has been done in a single object of key-value pairs to ensure the integrity of the object structure and to improve code readability.

mModels := map[string]models.EmployeeInterface{
    "key0": models.EmployeeInterface(&models.Employee{
        Department:  "department6",
        JoiningDay:  models.Days("Tuesday"),
        Salary:      66,
    }),
    "key1": models.EmployeeInterface(&models.Employee{
        Department:  "department7",
        JoiningDay:  models.Days("Saturday"),
        Salary:      88,
    }),
}

A Comparison of Complete Example Code

The following example shows the complete comparison between the code sample generation for initializing an employee model as an input parameter for the SendModel endpoint.

package main

import (
    "fmt"
    "log"
    "context"
    "tester"
    "time"
    "tester/models"
)

func main() {
    parseTime := func(layout, value string, errCallback func(error)) time.Time {
        dateTime, err := time.Parse(layout, value)
        if err != nil {
            errCallback(err)
       }
        return dateTime
    }
    client := tester.NewClient(
        tester.CreateConfiguration(),
    )
    bodyParamsController := client.BodyParamsController()

    ctx := context.Background()

    model := models.EmployeeInterface(&models.Employee{
        Department:  "department8",
        Dependents:  []models.PersonInterface{
            models.PersonInterface(&models.Person{
                Address:    "address4",
                Age:        int64(28),
                Birthday:   parseTime(models.DEFAULT_DATE, "2016-03-13T12:52:32.123Z", func(err error) { log.Fatalln(err) }),
                Name:       "name8",
                Uid:        "uid8",
                PersonType: models.ToPointer("Per"),
            }),
        },
        HiredAt:     parseTime(time.RFC1123, "Mon, 15 Jun 2009 20:45:30 GMT", func(err error) { log.Fatalln(err) }),
        JoiningDay:  models.Days("Monday"),
        Salary:      240,
        WorkingDays: []models.Days{
            models.Days("Thursday"),
            models.Days("Wednesday"),
            models.Days("Tuesday"),
        },
    })

    apiResponse, err := bodyParamsController.SendModel(ctx, model)
    if err != nil {
        log.Fatalln(err)
    } else {
        // Printing the result and response
        fmt.Println(apiResponse.Data)
        fmt.Println(apiResponse.Response.StatusCode)
    }
}

Bugs Fixed

  • Fixed indentation issues in the parameters initialization section.