Skip to content

Instantly share code, notes, and snippets.

@IndianGuru
Last active May 9, 2016 02:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IndianGuru/ac4e1d93751d42ac363f5318c407ad92 to your computer and use it in GitHub Desktop.
Save IndianGuru/ac4e1d93751d42ac363f5318c407ad92 to your computer and use it in GitHub Desktop.
Go program to use Watson
package main
import (
"fmt"
"io"
"log"
"net/http"
"os"
)
func main() {
myurl := fmt.Sprintf("https://stream.watsonplatform.net/text-to-speech/api/v1/synthesize?accept=audio/wav&voice=en-GB_KateVoice&text=The+Go+programming+language+rocks!")
// Create file to store the .wav file
output, err := os.Create("talk.wav")
if err != nil {
log.Fatal("Create: ", err)
return
}
defer output.Close()
// Build the request
req, err := http.NewRequest("GET", myurl, nil)
if err != nil {
log.Fatal("NewRequest: ", err)
return
}
// https://golang.org/pkg/net/http/#Request.SetBasicAuth
// SetBasicAuth sets the request's Authorization header to
// use HTTP Basic Authentication with the provided username and password.
req.SetBasicAuth("your username", "your password")
// For control over HTTP client headers,
// redirect policy, and other settings,
// create a Client
// A Client is an HTTP client
client := &http.Client{}
// Send the request via a client
// Do sends an HTTP request and
// returns an HTTP response
resp, err := client.Do(req)
if err != nil {
log.Fatal("Do: ", err)
return
}
// Callers should close resp.Body
// when done reading from it
// Defer the closing of the body
defer resp.Body.Close()
if _, err := io.Copy(output, resp.Body); err != nil {
log.Fatal("Copy: ", err)
return
}
fmt.Println("File talk.wav downloaded.")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment