I use “DescribeInstances” for this example.
First you have to find out the parameters for the API call. With VSCode you just klick on the documentation link of the import line.
There you navigate to the type client and find the call. If you know the aws cli call, which is:
aws ec2 describe-instances
You just change it to CamelCase DescribeInstances and delete the “-”.
The definition for this ec2 call is:
func (c *Client) DescribeInstances(ctx context.Context, params *DescribeInstancesInput, optFns ...func(*Options)) (*DescribeInstancesOutput, error)
Part | Meaning |
---|---|
(c *Client) | This is a function operating on the structure client |
DescribeInstances | Name of the function |
ctx context.Context | The context rarely needed, usually its *context.Todo() |
params *DescribeInstancesInput | type of the input variable called params |
optFns …func(*Options) | … means 0..n time this input parameter, most of the times not needed |
*DescribeInstancesOutput | Definition of the type you get in return |
error | Error, check for nil=ok |
Inside the structure in VSCode you get a code action, which fills all parameters for you.
The code action generates:
parms := &ec2.DescribeInstancesInput{
NextToken: new(string),
Filters: []types.Filter{},
InstanceIds: []string{},
MaxResults: new(int32),
NextToken: new(string),
}
In this example we just cut MaxResults to 10.
The parameters are documented in detail - just click on the documentation link inside VSCode (or other editors)
To code the parameters we add this import:
"github.com/aws/aws-sdk-go-v2/aws"
The aws package gives utility functions for all pointers.
Let me give you two examples:
A pointer to bool:
DryRun: new(bool),
// becomes
DryRun: aws.Bool(false),
A pointer to int32:
MaxResults: new(int32),
//becomes
MaxResults: aws.Int32(10),
So the parameter section for this example looks like:
parms := &ec2.DescribeInstancesInput{
MaxResults: aws.Int32(10),
}
Now that we have the parameters, we can call the service.
See the full source on github.