-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
59 lines (48 loc) · 946 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"runtime"
"strings"
"github.com/toqueteos/gatta"
)
func main() {
var key string
flag.StringVar(&key, "key", "", "Subscription Key, accessible from Azure's Translator Text API > Keys")
flag.Parse()
if len(key) != 32 {
fmt.Println("Invalid -key provided, it should be 32 characters long")
os.Exit(1)
}
tr, err := gatta.New(key)
fatalIf(err)
resp, err := tr.Translate("Hello World!", "es")
fatalIf(err)
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
fatalIf(err)
fmt.Println(string(body))
}
func fatalIf(err error) {
if err != nil {
file, line := caller()
fmt.Println(file, line, err)
os.Exit(1)
}
}
func caller() (file string, line int) {
var ok bool
_, file, line, ok = runtime.Caller(2)
if !ok {
file = "???"
line = 1
} else {
slash := strings.LastIndex(file, "/")
if slash >= 0 {
file = file[slash+1:]
}
}
return
}