Home > Backend Development > Golang > Go language implements Baidu translation API to realize translation between Lithuanian and Lithuanian

Go language implements Baidu translation API to realize translation between Lithuanian and Lithuanian

PHPz
Release: 2023-08-06 17:01:44
Original
1249 people have browsed it

Go language implements Baidu Translation API and realizes Lithuanian translation

Introduction:
With the process of globalization, language communication has become an important need. Many translation tools have emerged on the Internet, among which Baidu Translation API is one of the most popular translation APIs. This article will introduce how to use Go language to implement Baidu Translation API and realize the translation function of Lithuanian.

Step 1: Obtain Baidu Translation API Key
Before using Baidu Translation API, we need to register a Baidu Translation developer account and obtain the API key. The specific operations are as follows:

  1. Visit the official website of Baidu Translation Developer (http://api.fanyi.baidu.com/)
  2. Click "Developer Center" to enter the developer account registration page.
  3. After successful registration, log in to the Baidu Translation developer account and create an application.
  4. On the application management page, find the API key and record it.

Step 2: Create a Go language project and import related packages
Before starting, we need to create a Go language project and import related packages. The specific operations are as follows:

  1. Create a new folder, such as "BaiduTranslation".
  2. Create a file named "main.go" in this folder.
  3. Import the following packages in the "main.go" file:

    import (
     "fmt"
     "net/http"
     "io/ioutil"
     "encoding/json"
     "strings"
     "log"
    )
    Copy after login

Step 3: Write code to implement the translation function
After the relevant packages have been imported After that, we can start writing code to implement the translation function.

func main() {
    apiKey := "your_api_key" // 将此处替换为你自己的API密钥
    text := "Labas, pasauli" // 将此处替换为你想要翻译的中立陶宛文句子

    // 构建请求的URL
    url := fmt.Sprintf("http://api.fanyi.baidu.com/api/trans/vip/translate?q=%s&from=lt&to=zh&appid=%s&salt=1435660288&sign=%s",
        text, apiKey, getSign(text, apiKey))

    // 发送GET请求
    resp, err := http.Get(url)
    if err != nil {
        log.Fatalf("请求失败:%v", err)
    }
    defer resp.Body.Close()

    // 读取响应的body内容
    respBody, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        log.Fatalf("读取响应失败:%v", err)
    }

    // 解析JSON响应
    var result struct {
        TransResult []struct {
            Src string `json:"src"`
            Dst string `json:"dst"`
        } `json:"trans_result"`
    }
    err = json.Unmarshal(respBody, &result)
    if err != nil {
        log.Fatalf("解析JSON失败:%v", err)
    }

    // 输出翻译结果
    for _, trans := range result.TransResult {
        fmt.Printf("原文:%s
", trans.Src)
        fmt.Printf("译文:%s
", trans.Dst)
    }
}

// 计算请求签名
func getSign(text, apiKey string) string {
    appId := "your_app_id" // 将此处替换为你自己的APP ID
    salt := "1435660288" // 选择一个不重复的随机数
    signStr := fmt.Sprintf("%s%s%s%s", appId, text, salt, apiKey)
    sign := fmt.Sprintf("%X", md5.Sum([]byte(signStr)))
    return sign
}
Copy after login

Step 4: Run the code and view the results
After writing the code, we can run the code through the command line and view the translation results.

First, open a terminal and switch to the project root directory, then run the following command to compile and run the code:

go run main.go
Copy after login

In the command line window, you will see the following output:

原文:Labas, pasauli
译文:你好, 世界
Copy after login

So far, we have successfully implemented the Baidu Translation API using Go language and implemented the translation function of Lithuanian and Lithuanian.

Summary:
This article introduces how to use Go language to implement Baidu Translation API and implement the translation function of Lithuanian and Lithuanian. Through this example, we can see that it is very convenient to use Go language to handle HTTP requests and JSON parsing. I hope this article can help you quickly get started using Go language to implement other translation functions.

The above is the detailed content of Go language implements Baidu translation API to realize translation between Lithuanian and Lithuanian. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template