Perform Sentiment Analysis and Classification on Text in Go

Cloudmersive
1 min readMar 8, 2022

Sometimes our own biases & preconceived notions make it difficult to aptly judge content. Thank goodness there’s an API for that: you can find out if input text is positive, negative or neutral with the Sentiment Analysis & Classification iteration of our Natural Language Processing API. Use code examples below to integrate this API into your Go project, or head to the Cloudmersive API Console to find code in 13 different programming languages for your convenience.

Copy in the following if you’re working in Go, and include your API key (easily attainable by making a free account on www.cloudmersive.com) and text string where indicated:

package main
package main
import (
"fmt"
"strings"
"net/http"
"io/ioutil"
)
func main() {url := "https://api.cloudmersive.com/nlp-v2/analytics/sentiment"
method := "POST"
payload := strings.NewReader("TextToAnalyze=%3Cstring%3E")client := &http.Client {
}
req, err := http.NewRequest(method, url, payload)
if err != nil {
fmt.Println(err)
return
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("Apikey", "YOUR-API-KEY-HERE")
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(body))
}

--

--

Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.