内容简介:考六级,复习单词必备!!!
考六级,复习单词必备!!!
package main
import (
"bytes"
"crypto/md5"
"encoding/binary"
"encoding/hex"
"fmt"
"github.com/bitly/go-simplejson"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
func Md5(s string) string {//计算md5的值
h := md5.New()
h.Write([]byte(s))
return hex.EncodeToString(h.Sum(nil))
}
func u2s(form string) (to string, err error) {//unicode转字符串
bs, err := hex.DecodeString(strings.Replace(form, `\u`, ``, -1))
if err != nil {
return
}
for i, bl, br, r := 0, len(bs), bytes.NewReader(bs), uint16(0); i < bl; i += 2 {
binary.Read(br, binary.BigEndian, &r)
to += string(r)
}
return
}
func translate(word string) []byte {//调用api进行翻译
data := make(url.Values)
data["q"] = []string{word}
data["from"] = []string{"auto"}
data["to"] = []string{"auto"}
data["appid"] = []string{"*****************"}
data["salt"] = []string{"65"}
s := "*****************" + word + "65" + "*******************"//密匙
sign := Md5(s)
data["sign"] = []string{sign}
res, err := http.PostForm("http://api.fanyi.baidu.com/api/trans/vip/translate", data)
if err != nil {
fmt.Println(err.Error())
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
//str := string(body)
return body
}
func trans(words string) string {//翻译函数
body := translate(words)
js, err := simplejson.NewJson(body)
if err != nil {
panic(err.Error())
}
dst := js.Get("trans_result").GetIndex(0).Get("dst").MustString()
return dst
}
func main() {
words := "abandon"
dst := trans(words)
fmt.Println(dst)//打印结果
}
以上所述就是小编给大家介绍的《golang调用baidu翻译api实现自动翻译》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Programming Computer Vision with Python
Jan Erik Solem / O'Reilly Media / 2012-6-22 / USD 39.99
If you want a basic understanding of computer vision's underlying theory and algorithms, this hands-on introduction is the ideal place to start. As a student, researcher, hacker, or enthusiast, you'll......一起来看看 《Programming Computer Vision with Python》 这本书的介绍吧!
RGB转16进制工具
RGB HEX 互转工具
随机密码生成器
多种字符组合密码