Find answers to frequently asked questions about integrating and using Alibaba Cloud SDK for Go.
Prerequisites
-
Go 1.10.x or later is installed.
-
Make sure that Alibaba Cloud APIs are reachable over your network.
Overview
Problems and solutions
How do I handle AccessKey errors?
The following error message is returned when the AccessKey pair is not correctly configured:
-
Alibaba Cloud SDK V2.0: InvalidCredentials: Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly.
-
Alibaba Cloud SDK V1.0: SDK.ServerError InvalidAccessKeyId.NotFound Specified access key is not found.
Solutions:
-
Run the following commands to check whether the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables are configured.
Linux/macOS
echo $ALIBABA_CLOUD_ACCESS_KEY_ID echo $ALIBABA_CLOUD_ACCESS_KEY_SECRETWindows
echo %ALIBABA_CLOUD_ACCESS_KEY_ID% echo %ALIBABA_CLOUD_ACCESS_KEY_SECRET%If a valid AccessKey pair is returned, the environment variables are properly configured. If no AccessKey pair or an invalid AccessKey pair is returned, configure the environment variables as required. For more information, see Configure environment variables in Linux, macOS, and Windows.
-
Check for errors related to the AccessKey pair in the code.
Sample error request:
config := &openapi.Config{ AccessKeyId: tea.String(os.Getenv("yourAccessKeyID")), AccessKeySecret: tea.String(os.Getenv("yourAccessKeySecret")), }Sample success request:
config := &openapi.Config{ AccessKeyId: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")), AccessKeySecret: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")), }Noteos.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID") and os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET") retrieve the AccessKey ID and AccessKey secret from the corresponding environment variables.
ImportantTo prevent security risks, do not hardcode the AccessKey pair in your code.
Which error codes are returned if Alibaba Cloud SDKs cannot connect to Alibaba Cloud services?
One of the following error codes can be returned:
-
InvalidAccessKeyId: Check whether your AccessKey ID is valid.
-
SignatureDoesNotMatch: Check whether your AccessKey secret is valid.
Check your network connection and make sure that requests are not blocked by firewalls.
What do I do if the API request times out and the "*net.DNSError" or "net.OpError" error is reported?
API request timeouts can have multiple causes. The following are common causes and solutions:
Network connection issues
Cause: The network connection between the client and server is unstable or has failed, preventing the request from reaching the server.
Solutions:
Run the ping or curl command to test the connectivity between the on-premises host and the endpoint of the cloud service. For example, run the ping dysmsapi.aliyuncs.com or curl -v https://dysmsapi.aliyuncs.com command to test the connectivity between your on-premises host and the endpoint of the Short Message Service (SMS) API.
-
If the command times out or does not receive a response, check for blocking policies on your on-premises firewall or routers.
-
If a response is returned, we recommend that you specify a proper timeout period to prevent request failures caused by improper timeout configurations. For more information, see Configure a timeout period. Sample code:
// Create a RuntimeOptions instance and specify runtime parameters. runtime := &util.RuntimeOptions{} // Configure a timeout period for connection requests. Unit: milliseconds. runtime.ConnectTimeout = tea.Int(10000) // Set the timeout period for connection requests to 10 seconds.
Long processing time of the API request
Cause: The API request processing time exceeds the specified read timeout period.
Solution: Increase the read timeout period. For more information, see Configure a timeout period. Sample code:
// Create a RuntimeOptions instance and specify runtime parameters.
runtime := &util.RuntimeOptions{}
// Configure a timeout period for read requests. Unit: milliseconds.
runtime.ReadTimeout = tea.Int(10000) // Set the timeout period for read requests to 10 seconds.
What do I do if compilation fails or the "missing go.sum entry" error message is returned due to dependency version conflicts between different dependency libraries?
This error occurs because a required dependency is missing from the go.sum file. The go.mod file manages project dependencies. Ensure that no dependency version conflicts exist in go.mod, then run the following command to update dependencies and synchronize the go.mod and go.sum files:
go mod tidy
How do I use an SDK for Go in an existing project?
-
Open VS Code. In the top navigation bar, choose File > Open Folder. Create and select a project folder or select an existing project folder. In this example, a folder named gosdkproject is created and selected.
-
In the top navigation bar, choose Terminal > New Terminal. The TERMINAL window appears in the lower part of the console. Run the
go mod init gosdkprojectscommand in the TERMINAL window to initialize the Go project. After the Go project is initialized, a go.mod file is generated in the current project directory. A go.mod file is a module file in a Go project and is used to manage the dependencies and version information of the project.
-
Go to SDK Center and select the cloud product for which you want to use the SDK. Set SDK version to V2.0 and set the language to Go. Copy the installation command, paste it into the terminal, and press Enter to run it.
What do I do if the "MissingRequiredParameter" error is reported when I call an API operation?
The following example uses the SendSms operation of Short Message Service (SMS).
-
Go to the API Debugging page in the OpenAPI Developer Portal and select the cloud product and API operation.
-
Check whether the required parameters such as PhoneNumbers and SignName are specified in the constructed request object. In this example, the request object is
SendSmsRequest. -
Verify that all the required parameters are specified based on the API reference.
-
Make sure that the values of the required parameters are valid. For example, check whether mobile numbers are specified in valid formats.
-
The SDK automatically validates parameters before sending an API request. If a required parameter is missing, an error such as
MissingRequiredParameteris reported. For example, if the phone_numbers parameter is not specified, the "MissingPhoneNumbers: code: 400" error is reported. Specify the missing parameter based on the error message.
sendSmsRequest := &dysmsapi20170525.SendSmsRequest{
// The code of the SMS template.
TemplateCode: tea.String("<YOUR_VALUE>"),
// The variables of the SMS template. Example: {\"code\":\"1234\"}.
TemplateParam: tea.String("{\"code\":\"1234\"}"),
// The mobile numbers to which you want to send a text message.
PhoneNumbers: tea.String("<YOUR_VALUE>"),
// The name of the SMS signature.
SignName: tea.String("<YOUR_VALUE>"),
}
What do I do if I fail to call an API operation because the operation is not supported in the specified region and the "404 Not Found" message is returned?
Ensure that the selected region supports the service you are calling. For example, for Short Message Service (SMS), you can find the endpoint on its product page in the OpenAPI Developer Portal.
On the product page, in the Service Area List section, check the region ID and endpoint columns to confirm the endpoint for your target region. For example, for Short Message Service (SMS) (API version 2017-05-25), the endpoint for most regions is dysmsapi.aliyuncs.com.
Question 8: What do I do if the "go: go.mod file not found in current directory or any parent directory." message is returned when I run the go get command?
This message indicates that no go.mod file exists in the current directory or any parent directory. The go.mod file manages project dependencies and versions. Run the following command to initialize the go.mod file:
# Initialize a new go.mod file in the current directory and define module names. In most cases, a module name is the URL path to a repository. In this example, the domain name is example.com and the project name is goproject.
go mod init example.com/goproject
Checklist of basic Go exceptions
|
Error message |
Cause |
Solution |
|
Nil pointer dereference |
A null pointer is dereferenced, or a method is called on a null pointer. |
Before you use the pointer, make sure that the pointer is not nil. You can use a conditional statement or an error handling mechanism to check whether the pointer is nil. |
|
Invalid memory address or nil pointer dereference |
An invalid memory address is accessed, or a null pointer is dereferenced. |
Before you access the memory address, make sure that the memory address is valid and memory is allocated to the memory address. You can use a conditional statement or an error handling mechanism to check whether the memory address is valid. |
|
Timeouts and cancelations |
A network request or an operation times out or is canceled. |
Before you send a network request or perform a time-consuming operation, configure an appropriate timeout period and cancel the network request or operation based on your business requirements. You can use the context package to manage the timeout period and cancellation operations. |
Technical support
If the preceding FAQ does not resolve your issue, contact Alibaba Cloud technical support:
-
Submit a ticket: Submit a ticket on the Alibaba Cloud website.
-
If you have questions or feedback, contact Alibaba Cloud technical support in the DingTalk group (ID: 60965016010).