Do you want need a quick solution without going into the hassle of setting up a Database? If your answer to any of those questions was a yes, then youβve come to the right place. This post will show you how you can use Google sheets as your database.
For the purposes of this blogpost I will be usiing this Google sheet.
As you can see, we will be collecting the following data from the user β Name, Email and Age.
Create the API
Go to the google sheet you want to use.
Create column headers in the first column
Click on tools> script editor
Copy the following code to the editor
Click on run>run function> setup.
Now publish your script to get the request URL with the following settings.
In this post we will build Β simple REST APIβs using the Go programming language. We will also be using the MUX Router. I will also explain some of the fundamentals of the language for beginners.
REST: a RESTful API uses HTTP requests to GET, PUT, POST and DELETE data.
RESTful API designing: guidelines is a must read before you continue. It talks about terminologies, endpoints, versioning status codes and so much more.
Test your environment
Let us first test the environment to check if everything is working fine. For that we will be using a simple βHello Worldβ program.
Once that is done, let us import necessary packages.
Performing imports
Let us look at the imports used one by one.
encoding/json β since our APIβs communications will be handled in JSON format
log β will log errors
net/http β We will use this package to create the APIβs and communicate using HTTP protocols.
mux β Β A powerful URL router and dispatcher for golang . A router is used to define which function will run when a particular endpoint(URL) is called.
Writing the main funciton
Do note Β In Go, := is for declaration + assignment, whereas = is for assignment only.For example, var foo int = 10 is the same as foo := 10.
First we create a new variable for our multiplexer.
Then we use HandleFunc to define which function will handle which API endpoint.
With http.ListenAndServe we define the port that your program must listen to continuously.We wrap that around log.Fatal so that all exeptions are logged.
To run your code type the following in your console go run main.go
If you face an error telling you that mux is not installed then run go get -u github.com/gorilla/mux in your console.