Hey there, fellow Go enthusiast! Ready to dive into the world of customer feedback with Delighted? In this guide, we'll walk through building a slick integration with the Delighted API using Go. Whether you're looking to automate surveys or pull insights programmatically, you're in the right place. Let's get cracking!
Before we jump in, make sure you've got:
Let's kick things off by setting up our project:
mkdir delighted-go-integration cd delighted-go-integration go mod init delighted-integration
Now, let's grab the Delighted Go client:
go get github.com/delighted/delighted-go
Time to get our hands dirty with some code. Create a main.go
file and let's set up our client:
package main import ( "fmt" "github.com/delighted/delighted-go" ) func main() { client := delighted.NewClient("YOUR_API_KEY") // We'll add more code here soon! }
Replace YOUR_API_KEY
with your actual Delighted API key. Easy peasy!
Let's add a function to create a survey response:
func createSurveyResponse(client *delighted.Client) { resp, err := client.CreateSurveyResponse(&delighted.SurveyResponseParams{ Score: 9, Comment: "Loving this Go integration!", }) if err != nil { fmt.Printf("Error creating survey response: %v\n", err) return } fmt.Printf("Created survey response: %+v\n", resp) }
Now, let's fetch some responses:
func getSurveyResponses(client *delighted.Client) { responses, err := client.ListSurveyResponses(&delighted.ListSurveyResponseParams{ PerPage: 10, }) if err != nil { fmt.Printf("Error retrieving survey responses: %v\n", err) return } for _, resp := range responses { fmt.Printf("Response: %+v\n", resp) } }
Let's get a list of people:
func listPeople(client *delighted.Client) { people, err := client.ListPeople(&delighted.ListPeopleParams{ PerPage: 10, }) if err != nil { fmt.Printf("Error listing people: %v\n", err) return } for _, person := range people { fmt.Printf("Person: %+v\n", person) } }
Here's how to create or update a person:
func createOrUpdatePerson(client *delighted.Client) { person, err := client.CreateOrUpdatePerson(&delighted.PersonParams{ Email: "[email protected]", Name: "Gopher", }) if err != nil { fmt.Printf("Error creating/updating person: %v\n", err) return } fmt.Printf("Created/Updated person: %+v\n", person) }
Finally, let's unsubscribe someone:
func unsubscribePerson(client *delighted.Client) { err := client.Unsubscribe("[email protected]") if err != nil { fmt.Printf("Error unsubscribing person: %v\n", err) return } fmt.Println("Person unsubscribed successfully") }
As you've seen, we're checking for errors after each API call. This is crucial! Also, keep an eye on rate limits. The Delighted API has some restrictions, so consider implementing retries with exponential backoff for production use.
Now that we've got our functions, let's tie it all together in our main
function:
func main() { client := delighted.NewClient("YOUR_API_KEY") createSurveyResponse(client) getSurveyResponses(client) listPeople(client) createOrUpdatePerson(client) unsubscribePerson(client) }
Run your code with go run main.go
and watch the magic happen!
And there you have it! You've just built a Delighted API integration in Go. Pretty cool, right? You can now automate surveys, manage contacts, and pull data like a pro. As you get more comfortable, try expanding on this foundation – maybe add some command-line flags or build a simple web interface.
Now go forth and delight your users with your newfound powers! Happy coding, Gophers! 🐹✨