Welcome to Hack the North 2023! We're Angela and Adison, your workshop leads. Get a head start on your hackathon project by building a REST API with Go! In this workshop, you will gain experience creating web applications that can communicate with other services and exchange data—a fundamental skill—all while learning Golang, a very powerful and in-demand programming language.
- REST is a widely used architectural style for creating web applications that can communicate with other services and exchange data.
- Go has gained significant popularity in recent years due to its simplicity, performance, and built-in support for concurrency.
- By participating in this workshop, hackers can learn the fundamentals of Go by using it to build a REST API and understand its unique features, enabling them to work with the language effectively in their hackathon projects or other tech endeavors.
- Giving Go a Go: Build a REST API with Golang!
- Table of Contents
- Before You Start
- Code from Slides
- Resources
- Closing Words
Please make sure that you've checked out the Hack Pack for our workshop and follow through the doc to get yourself set up for success.
For easy access, here are the slides!
func getHackathons(c *gin.Context) {
var hackathons []Hackathon
db.Find(&hackathons)
c.IndentedJSON(http.StatusOK, gin.H{"data": hackathons})
}
func getHackathonById(c *gin.Context) {
var hackathon Hackathon
err := db.Where("id = ?", c.Param("id")).First(&hackathon).Error
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Hackathon not found!"})
return
}
c.JSON(http.StatusOK, gin.H{"data": hackathon})
}
Create this struct
underneath Hackathon
:
type HackathonInput struct {
Name string `json:"name" validate:"required"`
Date string `json:"date" validate:"required"`
Url string `json:"url" validate:"required"`
Location string `json:"location" validate:"required"`
}
In createHackathon
, write the code below
func createHackathon(c *gin.Context) {
var input HackathonInput
err := c.ShouldBindJSON(&input)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
err = validate.Struct(input)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
var hackathons []Hackathon
hackathon := Hackathon{
Id: int(db.Find(&hackathons).RowsAffected) + 1,
Name: input.Name,
Date: input.Date,
Url: input.Url,
Location: input.Location,
}
db.Create(&hackathon)
c.JSON(http.StatusOK, gin.H{"data": hackathon})
}
func updateHackathon(c *gin.Context) {
var hackathon Hackathon
err := db.Where("id = ?", c.Param("id")).First(&hackathon).Error
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Hackathon not found!"})
return
}
var input HackathonInput
err = c.ShouldBindJSON(&input)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
err = validate.Struct(input)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
db.Model(&hackathon).Updates(input)
c.JSON(http.StatusOK, gin.H{"data": hackathon})
}
func deleteHackathon(c *gin.Context) {
var hackathon Hackathon
err := db.Where("id = ?", c.Param("id")).First(&hackathon).Error
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Hackathon not found!"})
return
}
db.Delete(&hackathon)
c.JSON(http.StatusOK, gin.H{"data": true})
}
To import the Postman requests used in this workshop:
- Open Postman and navigate to “Workspaces” -> “My Workspace” (or create a workspace if there are none listed)
- In “Collections”, click “Import”
- Import the file
giving-go-a-go-workshop.postman_collection.json
from thepostman-requests
folder in this repo
To continue learning about Go, check out these resources:
- The official Golang website (with documentation and their own Learn section!)
- An interactive introduction to Go
- An Introduction to Programming in Go
Thank you so much for checking us out at Hack The North 2023, or on the interwebs. We hope you had as much fun as we did making this workshop. We'd love to see what you end up coming up with on your own!
With love, Angela and Adison