Google Translate CLI

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
5
down vote

favorite
2












A Translator in the Terminal using Golang. It uses Google's API. Currently, it only translates en (English) to es (Spanish).



package main

import (
"bufio"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
)

func (item *Result) UnmarshalJSON(data byte) error
var v interface
if err := json.Unmarshal(data, &v); err != nil
fmt.Println(err)
return err

item.Data = v[0].(interface).(interface)[0].(interface)[0].(string)
return nil


type Result struct
Data interface


func main()
fmt.Printf("nEnter textt: ")
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
query := scanner.Text()
response, err := http.Get("https://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=es&dt=t&q=" + url.QueryEscape(query))
if err != nil
fmt.Println(err)
os.Exit(1)
else
defer response.Body.Close()
bytes, err := ioutil.ReadAll(response.Body)
if err != nil
fmt.Println(err)
os.Exit(1)

var result Result
if err := json.Unmarshal(bytes, &result); err != nil
fmt.Println(err)

value := result.Data
fmt.Printf("Translationt: %sn", value)








share|improve this question



























    up vote
    5
    down vote

    favorite
    2












    A Translator in the Terminal using Golang. It uses Google's API. Currently, it only translates en (English) to es (Spanish).



    package main

    import (
    "bufio"
    "encoding/json"
    "fmt"
    "io/ioutil"
    "net/http"
    "net/url"
    "os"
    )

    func (item *Result) UnmarshalJSON(data byte) error
    var v interface
    if err := json.Unmarshal(data, &v); err != nil
    fmt.Println(err)
    return err

    item.Data = v[0].(interface).(interface)[0].(interface)[0].(string)
    return nil


    type Result struct
    Data interface


    func main()
    fmt.Printf("nEnter textt: ")
    scanner := bufio.NewScanner(os.Stdin)
    scanner.Scan()
    query := scanner.Text()
    response, err := http.Get("https://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=es&dt=t&q=" + url.QueryEscape(query))
    if err != nil
    fmt.Println(err)
    os.Exit(1)
    else
    defer response.Body.Close()
    bytes, err := ioutil.ReadAll(response.Body)
    if err != nil
    fmt.Println(err)
    os.Exit(1)

    var result Result
    if err := json.Unmarshal(bytes, &result); err != nil
    fmt.Println(err)

    value := result.Data
    fmt.Printf("Translationt: %sn", value)








    share|improve this question























      up vote
      5
      down vote

      favorite
      2









      up vote
      5
      down vote

      favorite
      2






      2





      A Translator in the Terminal using Golang. It uses Google's API. Currently, it only translates en (English) to es (Spanish).



      package main

      import (
      "bufio"
      "encoding/json"
      "fmt"
      "io/ioutil"
      "net/http"
      "net/url"
      "os"
      )

      func (item *Result) UnmarshalJSON(data byte) error
      var v interface
      if err := json.Unmarshal(data, &v); err != nil
      fmt.Println(err)
      return err

      item.Data = v[0].(interface).(interface)[0].(interface)[0].(string)
      return nil


      type Result struct
      Data interface


      func main()
      fmt.Printf("nEnter textt: ")
      scanner := bufio.NewScanner(os.Stdin)
      scanner.Scan()
      query := scanner.Text()
      response, err := http.Get("https://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=es&dt=t&q=" + url.QueryEscape(query))
      if err != nil
      fmt.Println(err)
      os.Exit(1)
      else
      defer response.Body.Close()
      bytes, err := ioutil.ReadAll(response.Body)
      if err != nil
      fmt.Println(err)
      os.Exit(1)

      var result Result
      if err := json.Unmarshal(bytes, &result); err != nil
      fmt.Println(err)

      value := result.Data
      fmt.Printf("Translationt: %sn", value)








      share|improve this question













      A Translator in the Terminal using Golang. It uses Google's API. Currently, it only translates en (English) to es (Spanish).



      package main

      import (
      "bufio"
      "encoding/json"
      "fmt"
      "io/ioutil"
      "net/http"
      "net/url"
      "os"
      )

      func (item *Result) UnmarshalJSON(data byte) error
      var v interface
      if err := json.Unmarshal(data, &v); err != nil
      fmt.Println(err)
      return err

      item.Data = v[0].(interface).(interface)[0].(interface)[0].(string)
      return nil


      type Result struct
      Data interface


      func main()
      fmt.Printf("nEnter textt: ")
      scanner := bufio.NewScanner(os.Stdin)
      scanner.Scan()
      query := scanner.Text()
      response, err := http.Get("https://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=es&dt=t&q=" + url.QueryEscape(query))
      if err != nil
      fmt.Println(err)
      os.Exit(1)
      else
      defer response.Body.Close()
      bytes, err := ioutil.ReadAll(response.Body)
      if err != nil
      fmt.Println(err)
      os.Exit(1)

      var result Result
      if err := json.Unmarshal(bytes, &result); err != nil
      fmt.Println(err)

      value := result.Data
      fmt.Printf("Translationt: %sn", value)










      share|improve this question












      share|improve this question




      share|improve this question








      edited May 16 at 1:34









      Jamal♦

      30.1k11114225




      30.1k11114225









      asked May 15 at 5:49









      45d7

      512




      512




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          4
          down vote













          In several places you use fmt.Println followed by os.Exit.
          You may create your own Exit function to write error message to stderr and terminate.



          Note: it is common practice to write error messages to stderr.
          stdout is for program output - translated text in your case.




          item.Data = v[0].(interface).(interface)[0].(interface)[0].(string)


          This statement will trigger a panic someday. I bet you need some recover.




          if err := json.Unmarshal(bytes, &result); err != nil {


          Hm, you've shaded previous err. Do you need one more error variable?
          When you use := operator, it will declare all missing variables on the left that doesnt present in current scope. Example:



          str := "What??"
          if str != ""
          str := ""
          fmt.Printf("str is '%s'n", str)

          if str != "" panic(str)



          Other notes:



          • I see you've used bufio.Scanner to get first line from input. You may consider reading all via ioutil.ReadAll. This way one may pipe some text to your program: cat ./text.txt | go-translate

          • No need for else branch in main function


          • value variable is used only once. You may pass result.Data directly to fmt.Printf call.

          Example:



          func Exit(err interface) 
          fmt.Fprint(os.Stderr, err)
          os.Exit(1)


          func main()
          fmt.Fprint(os.Stderr, "nEnter textt: ")

          scanner := bufio.NewScanner(os.Stdin)
          scanner.Scan()
          query := scanner.Text()

          response, err := http.Get("https://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=es&dt=t&q=" + url.QueryEscape(query))
          if err != nil
          Exit(err)

          defer response.Body.Close()

          bytes, err := ioutil.ReadAll(response.Body)
          if err != nil
          Exit(err)


          var result Result
          if err = json.Unmarshal(bytes, &result); err != nil
          Exit(err)


          fmt.Printf("Translationt: %sn", result.Data)






          share|improve this answer





















            Your Answer




            StackExchange.ifUsing("editor", function ()
            return StackExchange.using("mathjaxEditing", function ()
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            );
            );
            , "mathjax-editing");

            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "196"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );








             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f194426%2fgoogle-translate-cli%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            4
            down vote













            In several places you use fmt.Println followed by os.Exit.
            You may create your own Exit function to write error message to stderr and terminate.



            Note: it is common practice to write error messages to stderr.
            stdout is for program output - translated text in your case.




            item.Data = v[0].(interface).(interface)[0].(interface)[0].(string)


            This statement will trigger a panic someday. I bet you need some recover.




            if err := json.Unmarshal(bytes, &result); err != nil {


            Hm, you've shaded previous err. Do you need one more error variable?
            When you use := operator, it will declare all missing variables on the left that doesnt present in current scope. Example:



            str := "What??"
            if str != ""
            str := ""
            fmt.Printf("str is '%s'n", str)

            if str != "" panic(str)



            Other notes:



            • I see you've used bufio.Scanner to get first line from input. You may consider reading all via ioutil.ReadAll. This way one may pipe some text to your program: cat ./text.txt | go-translate

            • No need for else branch in main function


            • value variable is used only once. You may pass result.Data directly to fmt.Printf call.

            Example:



            func Exit(err interface) 
            fmt.Fprint(os.Stderr, err)
            os.Exit(1)


            func main()
            fmt.Fprint(os.Stderr, "nEnter textt: ")

            scanner := bufio.NewScanner(os.Stdin)
            scanner.Scan()
            query := scanner.Text()

            response, err := http.Get("https://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=es&dt=t&q=" + url.QueryEscape(query))
            if err != nil
            Exit(err)

            defer response.Body.Close()

            bytes, err := ioutil.ReadAll(response.Body)
            if err != nil
            Exit(err)


            var result Result
            if err = json.Unmarshal(bytes, &result); err != nil
            Exit(err)


            fmt.Printf("Translationt: %sn", result.Data)






            share|improve this answer

























              up vote
              4
              down vote













              In several places you use fmt.Println followed by os.Exit.
              You may create your own Exit function to write error message to stderr and terminate.



              Note: it is common practice to write error messages to stderr.
              stdout is for program output - translated text in your case.




              item.Data = v[0].(interface).(interface)[0].(interface)[0].(string)


              This statement will trigger a panic someday. I bet you need some recover.




              if err := json.Unmarshal(bytes, &result); err != nil {


              Hm, you've shaded previous err. Do you need one more error variable?
              When you use := operator, it will declare all missing variables on the left that doesnt present in current scope. Example:



              str := "What??"
              if str != ""
              str := ""
              fmt.Printf("str is '%s'n", str)

              if str != "" panic(str)



              Other notes:



              • I see you've used bufio.Scanner to get first line from input. You may consider reading all via ioutil.ReadAll. This way one may pipe some text to your program: cat ./text.txt | go-translate

              • No need for else branch in main function


              • value variable is used only once. You may pass result.Data directly to fmt.Printf call.

              Example:



              func Exit(err interface) 
              fmt.Fprint(os.Stderr, err)
              os.Exit(1)


              func main()
              fmt.Fprint(os.Stderr, "nEnter textt: ")

              scanner := bufio.NewScanner(os.Stdin)
              scanner.Scan()
              query := scanner.Text()

              response, err := http.Get("https://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=es&dt=t&q=" + url.QueryEscape(query))
              if err != nil
              Exit(err)

              defer response.Body.Close()

              bytes, err := ioutil.ReadAll(response.Body)
              if err != nil
              Exit(err)


              var result Result
              if err = json.Unmarshal(bytes, &result); err != nil
              Exit(err)


              fmt.Printf("Translationt: %sn", result.Data)






              share|improve this answer























                up vote
                4
                down vote










                up vote
                4
                down vote









                In several places you use fmt.Println followed by os.Exit.
                You may create your own Exit function to write error message to stderr and terminate.



                Note: it is common practice to write error messages to stderr.
                stdout is for program output - translated text in your case.




                item.Data = v[0].(interface).(interface)[0].(interface)[0].(string)


                This statement will trigger a panic someday. I bet you need some recover.




                if err := json.Unmarshal(bytes, &result); err != nil {


                Hm, you've shaded previous err. Do you need one more error variable?
                When you use := operator, it will declare all missing variables on the left that doesnt present in current scope. Example:



                str := "What??"
                if str != ""
                str := ""
                fmt.Printf("str is '%s'n", str)

                if str != "" panic(str)



                Other notes:



                • I see you've used bufio.Scanner to get first line from input. You may consider reading all via ioutil.ReadAll. This way one may pipe some text to your program: cat ./text.txt | go-translate

                • No need for else branch in main function


                • value variable is used only once. You may pass result.Data directly to fmt.Printf call.

                Example:



                func Exit(err interface) 
                fmt.Fprint(os.Stderr, err)
                os.Exit(1)


                func main()
                fmt.Fprint(os.Stderr, "nEnter textt: ")

                scanner := bufio.NewScanner(os.Stdin)
                scanner.Scan()
                query := scanner.Text()

                response, err := http.Get("https://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=es&dt=t&q=" + url.QueryEscape(query))
                if err != nil
                Exit(err)

                defer response.Body.Close()

                bytes, err := ioutil.ReadAll(response.Body)
                if err != nil
                Exit(err)


                var result Result
                if err = json.Unmarshal(bytes, &result); err != nil
                Exit(err)


                fmt.Printf("Translationt: %sn", result.Data)






                share|improve this answer













                In several places you use fmt.Println followed by os.Exit.
                You may create your own Exit function to write error message to stderr and terminate.



                Note: it is common practice to write error messages to stderr.
                stdout is for program output - translated text in your case.




                item.Data = v[0].(interface).(interface)[0].(interface)[0].(string)


                This statement will trigger a panic someday. I bet you need some recover.




                if err := json.Unmarshal(bytes, &result); err != nil {


                Hm, you've shaded previous err. Do you need one more error variable?
                When you use := operator, it will declare all missing variables on the left that doesnt present in current scope. Example:



                str := "What??"
                if str != ""
                str := ""
                fmt.Printf("str is '%s'n", str)

                if str != "" panic(str)



                Other notes:



                • I see you've used bufio.Scanner to get first line from input. You may consider reading all via ioutil.ReadAll. This way one may pipe some text to your program: cat ./text.txt | go-translate

                • No need for else branch in main function


                • value variable is used only once. You may pass result.Data directly to fmt.Printf call.

                Example:



                func Exit(err interface) 
                fmt.Fprint(os.Stderr, err)
                os.Exit(1)


                func main()
                fmt.Fprint(os.Stderr, "nEnter textt: ")

                scanner := bufio.NewScanner(os.Stdin)
                scanner.Scan()
                query := scanner.Text()

                response, err := http.Get("https://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=es&dt=t&q=" + url.QueryEscape(query))
                if err != nil
                Exit(err)

                defer response.Body.Close()

                bytes, err := ioutil.ReadAll(response.Body)
                if err != nil
                Exit(err)


                var result Result
                if err = json.Unmarshal(bytes, &result); err != nil
                Exit(err)


                fmt.Printf("Translationt: %sn", result.Data)







                share|improve this answer













                share|improve this answer



                share|improve this answer











                answered May 15 at 7:13









                sineemore

                1,183217




                1,183217






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f194426%2fgoogle-translate-cli%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    Python Lists

                    Aion

                    JavaScript Array Iteration Methods