Hello! In this post, I will explain the cost-effective method I use to host my Go web applications with varying levels of complexity, all starting from as low as $5 per month. This method also allows to easy deploy and scale your golang application.
As an example, this is how I host whattoreadafter.xyz, a service that recommends books based on the book you just finished reading, by using AI.
Starting off, let’s enumerate the tools we’ll be using alongside Golang:
In order to follow along, you will need to have a DigitalOcean account. If you don’t have one, you can sign up here and get $200 in credit over 60 days.
Let’s get started! For simplicity sake, our application will be a simple web server that returns current time in UTC format.
package main
import (
"fmt"
"net/http"
"time"
)
func currentTimeHandler(w http.ResponseWriter, r *http.Request) {
currentTime := time.Now().UTC()
fmt.Fprintf(w, "Current Time (UTC): %s", currentTime.Format(time.RFC3339))
}
func main() {
http.HandleFunc("/", currentTimeHandler)
fmt.Println("Server is running on port 8080")
http.ListenAndServe(":8080", nil)
}
Now let’s host it!
In order for you to easier follow along, I will list the steps we will take to host our application:
main.go
file.Example:
FROM golang:alpine AS builder
RUN apk add --no-cache --update \
git \
ca-certificates
ADD . /app
WORKDIR /app
COPY go.mod ./
RUN go mod download
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o /main .
FROM alpine
COPY --from=builder /main ./
RUN chmod +x ./main
ENTRYPOINT ["./main"]
That’s it! You have successfully deployed your Golang application!
You can also add your own domain name to your app by clicking on the “Settings” tab and then clicking on “Domains”.
Now, every time you push a new commit to your repository, DigitalOcean will automatically build and deploy your application. This is a great way to host your Golang applications, especially if you are just starting out and don’t want to spend a lot of money on hosting.
Thank you for reading! If you have any questions, feel free to reach out to me on Twitter.
Since Go 1.19 we can use a new 103 (Early Hints)
http status code when we create web applications. Let’s figure out how and when this could help us.
We are going to create a simple golang web server that servers some html content. One html page will be served with 103
header and another one without.
After loading comparison we will see how early hints can improve page performance.
I guess that almost everyone in the go community was exciting when Go 1.18 was released, especially because of generics. Some days ago I decided to try generics in the real-world application, by refactoring some of its pieces, related to a caching logic.
go generics redis cacheThis time, I will show you how to work with the maps in go effectively and prevent the occurrence of the data race errors. Data races happen when several goroutines access the same resource concurrently and at least one of the accesses is a write.
go concurrent map data raceRecently, I discovered a surprisingly reliable memory caching solution, which I’m planning to use in all my further applications to increase performance. In this blog post, I will share some code examples of how you can integrate Ristretto caching library into your application.
go caching ristretto performanceWhen we create a web application, usually, there a need to communicate with the users to inform them about the results of their actions. The easiest way to communicate - is to send messages. These messages might be warnings, errors, or just informational text. In this article, we will improve the UX of our user authentication application from the previous article by adding an error flash message when the user entered a wrong password and a success message after user authorisation.
go echo flash messages