# Responses

Every method returns a pointer to a struct decoded from the API's JSON. Fields are exported in Studly case: the API's `line_1` is `Line1` and `post_town` is `PostTown`. The JSON tag on each field gives the API's own name.

## Absent and null fields

A field the API may leave out or send as `null` is a pointer, nil when absent. Slices, maps and `any` fields are nil instead. Every other field is a plain value.

pointers.gogo

```go
package main

import (
	"context"
	"fmt"
	"log"
	"os"

	idealpostcodes "go.ideal-postcodes.co.uk/sdk"
)

func main() {
	client := idealpostcodes.NewClient("ak_test",
		idealpostcodes.WithManagementKey(os.Getenv("IDPC_MANAGEMENT_KEY")))
	res, err := client.KeyDetails(context.Background(), "ak_test")
	if err != nil {
		log.Fatal(err)
	}
	if limit := res.Result.DailyLimit.Limit; limit != nil {
		fmt.Println("daily limit", *limit)
	} else {
		fmt.Println("no daily limit")
	}
}
```

Request bodies follow the same rule: an optional body field is a pointer, set with `new(value)`, and nil sends nothing. Optional query parameters are plain values in a `*Params` struct, and a zero value is not sent.

## Raw JSON

Call `RawJSON()` on any response to get the body as the API sent it. Use it to read a field the SDK does not have yet, or to log the response.

raw\.gogo

```go
package main

import (
	"context"
	"fmt"
	"log"

	idealpostcodes "go.ideal-postcodes.co.uk/sdk"
)

func main() {
	client := idealpostcodes.NewClient("ak_test")
	res, err := client.ResolveAddress(context.Background(), "paf_2670849", nil)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(string(res.RawJSON()))
}
```

## Fields with more than one shape

Some results come in one of several forms. The SDK decodes the one that matches, and the field's type has an `As<Type>()` method per form, which returns nil for the others. `Variant()` returns the decoded form for a type switch, and `Raw()` the JSON as received.

| Field                            | `As` methods                                                                                      |
| -------------------------------- | ------------------------------------------------------------------------------------------------- |
| `FindAddress()` hits             | `AsUkAddressSuggestion()` with a `UDPRN`, or `AsAddressSuggestion()` for addresses outside the UK |
| `AddressCleanse()` result        | `AsGbrCleanseMatch()` with a `Match` address, or `AsGbrCleanseNoMatch()`                          |
| `EmailValidation()` result       | `AsEmail()`, or `AsUnknownEmail()` when deliverability cannot be checked                          |
| `PhoneNumberValidation()` result | `AsPhoneNumber()`, or `AsInvalidPhoneNumber()`                                                    |
| `FindPlace()` hits               | `AsPlaceSuggestion()`                                                                             |
| An address's `Native` record     | `AsPafAddress()`, `AsAbpAddress()` and one per dataset                                            |

variant.gogo

```go
package main

import (
	"context"
	"fmt"
	"log"

	idealpostcodes "go.ideal-postcodes.co.uk/sdk"
)

func main() {
	client := idealpostcodes.NewClient("ak_test")
	res, err := client.AddressCleanse(context.Background(),
		idealpostcodes.AddressCleanseBody{Query: "9 Malyons Rd, Swanley, BR8"}, nil)
	if err != nil {
		log.Fatal(err)
	}
	switch result := res.Result.Variant().(type) {
	case *idealpostcodes.GbrCleanseMatch:
		fmt.Println(result.Match.Postcode, "with confidence", result.Confidence)
	case *idealpostcodes.GbrCleanseNoMatch:
		fmt.Println("no match")
	}
}
```

## Strings or numbers

A few fields arrive as a string from one dataset and a number from another, such as `Longitude`, `Latitude` and `UDPRN` on some types. These are `idealpostcodes.Scalar`. `String()` returns the value as text, `Float64()` and `Int64()` parse it, and `IsNull()` reports a missing value.

## New API values

Your integration keeps working. Fields with a fixed set of values, such as `PremiseMatch` on a cleanse result, are plain strings, and their doc comments list the values known when the SDK was built. A value added later arrives unchanged, so compare against the values you handle and fall through for the rest.

Decoding never fails on shape. A field the API adds is ignored but kept in `RawJSON()`. A field of the wrong type, or a missing one, is left at its zero value. Errors come from HTTP and the network only. See [error handling](/docs/sdks/go/errors.md).

## Native record

`Native` on an address is the raw record from the address's source dataset, with a type named for the dataset: `PafAddress` for Royal Mail PAF, `AbpAddress` for AddressBase Premium, `UspsAddress` for the United States and so on. The `Dataset` field on the address says which. `Native` is nil when the API sends no record. A dataset the SDK does not know yet has a nil `Variant()`, and `Raw()` still holds the record, so a new dataset never breaks your integration.

A few native fields have names that start with something other than a Latin letter, such as the Korean `법정읍면동명`. Go only exports a name that starts with an upper-case letter, so these take an `X` prefix: `X법정읍면동명`.

The [data sources](/docs/data.md) pages document every dataset's fields.
