Table of Contents
Introduction
In this tutorial, we will learn how to deploy a Go web application on the Heroku platform. By the end of this tutorial, you will have a functioning Go web app deployed and accessible online. We will cover the necessary setup, creating a simple Go web app, and deploying it to Heroku.
Prerequisites
Before starting this tutorial, you should have the following:
- Basic knowledge of Go programming language
- Go installed on your machine
- An account on Heroku (https://www.heroku.com/)
- Heroku CLI (https://devcenter.heroku.com/categories/command-line)
Setup
- Install Go: Visit the official Go website (https://golang.org/) and download the appropriate installer for your operating system. Follow the installation instructions to install Go on your machine. Verify the installation by running
go version
in your terminal. - Sign up on Heroku: Go to the Heroku website (https://www.heroku.com/) and create an account if you don’t have one already.
-
Install Heroku CLI: Download and install the Heroku CLI from the official website (https://devcenter.heroku.com/categories/command-line). Follow the installation instructions for your operating system.
- Verify Heroku CLI installation: Open a new terminal window and run
heroku version
to verify that the Heroku CLI was installed successfully.
Creating a Go Web App
- Create a new directory for your Go web app: Open a terminal and navigate to the desired location for your project. Run
mkdir go-web-app
to create a new directory. - Initialize Go module: Go to the project directory by running
cd go-web-app
. Initialize the Go module by runninggo mod init github.com/YOUR_GITHUB_USERNAME/go-web-app
. ReplaceYOUR_GITHUB_USERNAME
with your actual GitHub username or choose a different module name. -
Create a main.go file: Run
touch main.go
to create a new main.go file in the project directory. Open the file in a code editor. -
Write a simple web app: In the main.go file, write the following code to create a basic Go web app:
package main import ( "fmt" "net/http" ) func helloHandler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello, Go Web App on Heroku!") } func main() { http.HandleFunc("/", helloHandler) http.ListenAndServe(":8080", nil) }
The code defines a
helloHandler
function that writes “Hello, Go Web App on Heroku!” as the response. In themain
function, we set up a HTTP route for the root path (“/”) and start the HTTP server on port 8080. - Test locally: To test the web app locally, run
go run main.go
in the terminal. Open a web browser and visithttp://localhost:8080
to see the “Hello, Go Web App on Heroku!” message.
Deploying to Heroku
- Login to Heroku CLI: In your terminal, run
heroku login
and follow the prompts to log in to your Heroku account. - Create a Heroku app: Run
heroku create
to create a new Heroku app. This will automatically set up a remote repository for your app. -
Deploy the app: Run
git push heroku master
to deploy your app to Heroku. Heroku will automatically build the app and start running it. -
View the app: After successfully deploying, run
heroku open
to open the app in your default browser. Alternatively, you can visit the app’s URL by runningheroku apps:info -s | grep web_url | cut -d= -f2
in the terminal.Congratulations! You have successfully deployed a Go web app on Heroku.
Conclusion
In this tutorial, you learned how to deploy a Go web application on Heroku. We covered the necessary setup, creating a simple Go web app, and deploying it to Heroku. Now you can continue building and expanding your Go web app with the knowledge of deploying it to a production environment.
Remember to check the Heroku documentation and Go web development best practices for further guidance on improving your app and scaling it as needed.
Happy coding!