go language implements Baidu Translation API to realize mutual translation between Chinese and German
Overview:
In today's era of globalization, the need for language communication is becoming more and more prominent. Baidu Translation API provides convenient online translation services, and Go language, as a fast and efficient programming language, can easily call Baidu Translation API. This article will introduce how to use Go language to write a simple program to realize the function of mutual translation between Chinese and German.
Preparation work:
Before starting to write code, you need to apply for a developer account of Baidu Translation API and obtain the corresponding application ID and key. For specific application steps, please refer to the official documentation of Baidu Translation API. After obtaining the application ID and key, we can start writing code.
Code example:
package main import ( "fmt" "io/ioutil" "net/http" "net/url" "strings" ) func main() { fromLang := "zh" // 源语言,中文 toLang := "de" // 目标语言,德文 query := "你好" // 要翻译的文本 appID := "your_app_id" // 替换为自己的应用ID appSecret := "your_app_key" // 替换为自己的应用密钥 apiURL := "https://fanyi-api.baidu.com/api/trans/vip/translate" data := url.Values{} data.Set("q", query) data.Set("from", fromLang) data.Set("to", toLang) data.Set("appid", appID) salt := "1435660288" data.Set("salt", salt) sign := appID + query + salt + appSecret // 计算签名 sign = strings.ToLower(sign) sign = fmt.Sprintf("%x", sha256.Sum256([]byte(sign))) data.Set("sign", sign) req, err := http.NewRequest("POST", apiURL, strings.NewReader(data.Encode())) if err != nil { fmt.Println("Error occurred while creating request:", err) return } req.Header.Set("Content-Type", "application/x-www-form-urlencoded") client := &http.Client{} resp, err := client.Do(req) if err != nil { fmt.Println("Error occurred while sending request:", err) return } body, err := ioutil.ReadAll(resp.Body) if err != nil { fmt.Println("Error occurred while reading response:", err) return } fmt.Println(string(body)) }
Running result example:
{ "from": "zh", "to": "de", "trans_result": [{ "src": "你好", "dst": "Hallo" }] }
Code analysis:
The above code sends a request to Baidu Translation API through HTTP POST request and obtains the translation result . Among them, we first set the source language, target language and text to be translated. Then, according to the requirements of the official documentation, we spliced the request URL and calculated the sign value. Next, we create an HTTP request and set the Content-Type of the request header. Then, send the request and read the result of the response. Finally, print the translation results.
Summary:
By using the Go language, we can easily call Baidu Translation API to achieve mutual translation between Chinese and German. In actual projects, we can make more expansions and optimizations as needed. I hope that the sample code in this article can be helpful to readers and promote further development in language communication.
The above is the detailed content of Go language implements Baidu translation API to realize mutual translation between Chinese and German. For more information, please follow other related articles on the PHP Chinese website!