Home >Backend Development >Golang >Using go language to develop Baidu Translation API to realize mutual translation between Chinese and Italian

Using go language to develop Baidu Translation API to realize mutual translation between Chinese and Italian

王林
王林Original
2023-08-12 16:57:281295browse

Using go language to develop Baidu Translation API to realize mutual translation between Chinese and Italian

Using Go language to develop Baidu Translation API to achieve mutual translation between Chinese and Italian

Abstract:
This article will introduce how to use Go language development and implement it through Baidu Translation API The function of translating between Chinese and Italian. We will use the API provided by the Baidu Translate developer platform to implement this function and provide corresponding code examples.

1. Preparation work
Before we start, we need to do some preparation work:

  1. Register a Baidu developer account. If you don’t have an account yet, you can register a developer account through the official website.
  2. Log in to the Baidu Developer Platform, create a new application in "My Applications", and generate API Key and Secret Key. This will be used for authentication when making requests to the Baidu Translate API.

2. Code Example
The following is a simple code example to demonstrate how to use Go language to call Baidu Translation API for text translation.

package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
    "net/url"
)

func main() {
    apiURL := "https://fanyi-api.baidu.com/api/trans/vip/translate"
    apiKey := "your_api_key"
    secretKey := "your_secret_key"

    // 要翻译的文本
    q := "你好"
    // 源语言和目标语言
    from := "zh"
    to := "it"

    // 构造请求URL
    urlStr := fmt.Sprintf("%s?q=%s&from=%s&to=%s&appid=%s&salt=%s&sign=%s",
        apiURL, url.QueryEscape(q), from, to, apiKey, "123456", sign(q, "123456", apiKey, secretKey))

    // 发送请求并获取响应
    resp, err := http.Get(urlStr)
    if err != nil {
        fmt.Println("请求百度翻译API失败:", err)
        return
    }
    defer resp.Body.Close()

    // 解析并打印翻译结果
    body, _ := ioutil.ReadAll(resp.Body)
    fmt.Println(string(body))
}

// 计算签名
func sign(q, salt, appID, secretKey string) string {
    signStr := appID + q + salt + secretKey
    return Md5(signStr)
}

// 计算字符串的MD5值
func Md5(str string) string {
    md5Ctx := md5.New()
    md5Ctx.Write([]byte(str))
    return hex.EncodeToString(md5Ctx.Sum(nil))
}

In the above code, we first set the URL of Baidu Translation API and the API Key and Secret Key generated when we created the application on Baidu Developer Platform. Next, we set up the text to be translated (i.e. the q variable), the source language, and the target language. We generate the signature of the request by calling the sign() function and construct the complete request URL. Finally, we send an HTTP GET request and get the translation results by reading the response.

3. Summary
This article introduces how to use Go language development to realize the function of mutual translation between Chinese and Italian through Baidu Translation API. We used the API provided by Baidu Translate developer platform and gave corresponding code examples. Readers can follow the instructions in the sample code to make corresponding modifications and extensions to meet their own needs. Through the introduction of this article, readers can better understand how to use Go language to develop and call external APIs, and how to implement text translation functions.

The above is the detailed content of Using go language to develop Baidu Translation API to realize mutual translation between Chinese and Italian. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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