Learning Go by examples: part 2 - Create an HTTP REST API Server in Go

This page summarizes the projects mentioned and recommended in the original post on dev.to

Our great sponsors
  • InfluxDB - Power Real-Time Data Analytics at Scale
  • WorkOS - The modern identity platform for B2B SaaS
  • SaaSHub - Software Alternatives and Reviews
  • For that, I logged in GitHub website, clicked on the repositories link, click on "New" green button and then I created a new repository called “learning-go-by-example”.

  • GoSwagger

    Swagger 2.0 implementation for go

  • We will install go-swagger tool, don't hesitate to follow installation page.

  • InfluxDB

    Power Real-Time Data Analytics at Scale. Get real-time insights from all types of time series data with InfluxDB. Ingest, query, and analyze billions of data points in real-time with unbounded cardinality.

    InfluxDB logo
  • swagger-ui

    Swagger UI is a collection of HTML, JavaScript, and CSS assets that dynamically generate beautiful documentation from a Swagger-compliant API.

  • It's true, we will now step up our HTTP server and use Swagger, which handles definitions of our HTTP endpoints.

  • gophers

    Gopher artwork (Golang mascot) (by scraly)

  • package main import ( "fmt" "log" "net/http" "github.com/go-openapi/loads" "github.com/go-openapi/runtime/middleware" "github.com/scraly/learning-go-by-examples/go-rest-api/pkg/swagger/server/restapi" "github.com/scraly/learning-go-by-examples/go-rest-api/pkg/swagger/server/restapi/operations" ) func main() { // Initialize Swagger swaggerSpec, err := loads.Analyzed(restapi.SwaggerJSON, "") if err != nil { log.Fatalln(err) } api := operations.NewHelloAPIAPI(swaggerSpec) server := restapi.NewServer(api) defer func() { if err := server.Shutdown(); err != nil { // error handle log.Fatalln(err) } }() server.Port = 8080 api.CheckHealthHandler = operations.CheckHealthHandlerFunc(Health) api.GetHelloUserHandler = operations.GetHelloUserHandlerFunc(GetHelloUser) api.GetGopherNameHandler = operations.GetGopherNameHandlerFunc(GetGopherByName) // Start server which listening if err := server.Serve(); err != nil { log.Fatalln(err) } } //Health route returns OK func Health(operations.CheckHealthParams) middleware.Responder { return operations.NewCheckHealthOK().WithPayload("OK") } //GetHelloUser returns Hello + your name func GetHelloUser(user operations.GetHelloUserParams) middleware.Responder { return operations.NewGetHelloUserOK().WithPayload("Hello " + user.User + "!") } //GetGopherByName returns a gopher in png func GetGopherByName(gopher operations.GetGopherNameParams) middleware.Responder { var URL string if gopher.Name != "" { URL = "https://github.com/scraly/gophers/raw/main/" + gopher.Name + ".png" } else { //by default we return dr who gopher URL = "https://github.com/scraly/gophers/raw/main/dr-who.png" } response, err := http.Get(URL) if err != nil { fmt.Println("error") } return operations.NewGetGopherNameOK().WithPayload(response.Body) }

  • Puts Debuggerer

    Ruby library for improved puts debugging, automatically displaying bonus useful information such as source line number and source code.

  • package main import ( "fmt" "log" "net/http" "github.com/go-openapi/loads" "github.com/go-openapi/runtime/middleware" "github.com/scraly/learning-go-by-examples/go-rest-api/pkg/swagger/server/restapi" "github.com/scraly/learning-go-by-examples/go-rest-api/pkg/swagger/server/restapi/operations" ) func main() { // Initialize Swagger swaggerSpec, err := loads.Analyzed(restapi.SwaggerJSON, "") if err != nil { log.Fatalln(err) } api := operations.NewHelloAPIAPI(swaggerSpec) server := restapi.NewServer(api) defer func() { if err := server.Shutdown(); err != nil { // error handle log.Fatalln(err) } }() server.Port = 8080 api.CheckHealthHandler = operations.CheckHealthHandlerFunc(Health) api.GetHelloUserHandler = operations.GetHelloUserHandlerFunc(GetHelloUser) api.GetGopherNameHandler = operations.GetGopherNameHandlerFunc(GetGopherByName) // Start server which listening if err := server.Serve(); err != nil { log.Fatalln(err) } } //Health route returns OK func Health(operations.CheckHealthParams) middleware.Responder { return operations.NewCheckHealthOK().WithPayload("OK") } //GetHelloUser returns Hello + your name func GetHelloUser(user operations.GetHelloUserParams) middleware.Responder { return operations.NewGetHelloUserOK().WithPayload("Hello " + user.User + "!") } //GetGopherByName returns a gopher in png func GetGopherByName(gopher operations.GetGopherNameParams) middleware.Responder { var URL string if gopher.Name != "" { URL = "https://github.com/scraly/gophers/raw/main/" + gopher.Name + ".png" } else { //by default we return dr who gopher URL = "https://github.com/scraly/gophers/raw/main/dr-who.png" } response, err := http.Get(URL) if err != nil { fmt.Println("error") } return operations.NewGetGopherNameOK().WithPayload(response.Body) }

  • HomeBrew

    🍺 The missing package manager for macOS (or Linux)

  • Install task in your local machine, in order to do that you can follow installation instructions or if you have a MacOS with homebrew, you can use brew install command:

NOTE: The number of mentions on this list indicates mentions on common posts plus user suggested alternatives. Hence, a higher number means a more popular project.

Suggest a related project

Related posts